Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/78.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何使用jquery将外部页面加载到div中?_Php_Jquery_Ajax_Html - Fatal编程技术网

Php 如何使用jquery将外部页面加载到div中?

Php 如何使用jquery将外部页面加载到div中?,php,jquery,ajax,html,Php,Jquery,Ajax,Html,我有一个有3个链接的菜单,当用户单击菜单链接时,我希望页面使用jquery加载到同一页面的div上,im使用php和mysql 谢谢大家! 看看这个函数。 我建议你读一些文件 顺便说一句,这可能是你的答案: $.ajax({ url: 'ajax/test.html', success: function(data) { $('.result').html(data); alert('Load was performed.'); } }); 从@jAndy的示例扩展

我有一个有3个链接的菜单,当用户单击菜单链接时,我希望页面使用jquery加载到同一页面的div上,im使用php和mysql

谢谢大家!

看看这个函数。


我建议你读一些文件

顺便说一句,这可能是你的答案:

$.ajax({
  url: 'ajax/test.html',
  success: function(data) {
    $('.result').html(data);
    alert('Load was performed.');
  }
});

从@jAndy的示例扩展而来:

标记:

<a href="/foo/bar.html" id="baz">Load external html</a>
<div id="result"></div>

您可以使用ajax从mysql数据库收集信息

$.ajax({
  type: 'post',
  url: 'getnames.php',
  datatype: "json",
  success: function(data) {
    $('#id_of_the_div').text("");
    $('#id_of_the_div').append(data.returned);
  }
});
在php文件中:

//..
// commands that run mysql queries and gather information from database
//..
//..
//..
// you store what you've got from the database in a $ret variable and then:
// $ret can be for example:
// $ret = "<table><tr><td>Peter</td><td>40</td></tr></table>";

$arr = Array("returned"=>$ret);
echo json_encode($arr);
/。。
//运行mysql查询并从数据库收集信息的命令
//..
//..
//..
//将从数据库获得的数据存储在$ret变量中,然后:
//$ret可以是例如:
//$ret=“Peter40”;
$arr=数组(“返回的”=>$ret);
echo json_编码($arr);

当然,这只是您可以使用的几种方法中的一个示例。我已经使用了ajax请求和json…

感谢您的回答,这是javascript方面的内容,我应该在html页面上为超链接添加什么内容,以将其连接到此代码段!!抱歉,我还是一个新手,您想在同一台服务器上加载什么页面(出于安全原因,Javascript阻止您从外部站点加载,因此您需要一个中间服务器端脚本从另一个站点获取数据,您可以使用ajax加载)?
$('#baz').click(function () { $('#result').load(this.href); });
$.ajax({
  type: 'post',
  url: 'getnames.php',
  datatype: "json",
  success: function(data) {
    $('#id_of_the_div').text("");
    $('#id_of_the_div').append(data.returned);
  }
});
//..
// commands that run mysql queries and gather information from database
//..
//..
//..
// you store what you've got from the database in a $ret variable and then:
// $ret can be for example:
// $ret = "<table><tr><td>Peter</td><td>40</td></tr></table>";

$arr = Array("returned"=>$ret);
echo json_encode($arr);