用javascript和jquery动态包含一个php文件 [英] Including a php file dynamically with javascript and jquery

查看:108
本文介绍了用javascript和jquery动态包含一个php文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想稍后(动态)包含一个php文件,而不是在顶部。所有这个文件都是从服务器获取一些内容并将其存储在我后来在jquery中使用的javascript变量中。

I want to include a php file later(dynamically), rather than at the top. All this file does is get some contents from server and stores it in a javascript variable that i later use in jquery.

基本上当你点击链接时,我得到的信息,然后显示它以节省资源,因为有许多链接可能没有使用。

basically when you click the link, i get the info, then display it to save resources because there are many links that may not be used.

我应该只做 $(body)。 load(phpfile.php); ?这应该工作,但我想找到一个更合适的方式,因为它与身体等html标签无关。

Should i just do $("body").load("phpfile.php"); ? this should work but I am trying to find a more proper way because it has nothing to do with html tag like body.

推荐答案

你正在以一种奇怪的方式解决问题。不是说它不会起作用,只是说你很难到达。我建议在json中使用jquery的ajax。

You are approaching the problem in an odd way. Not to say it wouldn't work, just that you would have a hard time getting to. I would recommend using jquery's ajax with json.

类似于:

< div id =output>< ; / div>

$(function ()
{
    //$.json('urltoRequestfrom?variable1=value1');
    $.post('/echo/json/',
           {json: '{"name":"test"}'},
           function (data)
           {
               $('#output').html(data["name"]); //first json object
           },'json');
});

这篇关于用javascript和jquery动态包含一个php文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆