Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/unix/3.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
使用Ajax刷新php页面中的所有div_Php_Html_Refresh - Fatal编程技术网

使用Ajax刷新php页面中的所有div

使用Ajax刷新php页面中的所有div,php,html,refresh,Php,Html,Refresh,我有一个包含9个不同div的php页面,这些div在从mysql数据库重新加载每个页面后都会更新。我想在不刷新整个页面的情况下自动完成。可能吗?我知道这可以通过Ajax实现,但我不明白我是如何做到的。有谁能告诉我这方面的好教程吗?如果我没弄错你的问题的话 window.setInterval(function(){ // call your function here // make tha ajax call here to update the divs // this func

我有一个包含9个不同div的php页面,这些div在从mysql数据库重新加载每个页面后都会更新。我想在不刷新整个页面的情况下自动完成。可能吗?我知道这可以通过Ajax实现,但我不明白我是如何做到的。有谁能告诉我这方面的好教程吗?

如果我没弄错你的问题的话

window.setInterval(function(){
  // call your function here
  // make tha ajax call here to update the divs
  // this function will run every 5 secs and will make the AJAX call
  // write the function here to update the divs
}, 5000);
阅读下文


如果不想不断更新页面,只需调用AJAX即可更新div。

使用jQuery有一种简单有效的方法:

概述: 在您直接调用ajax的脚本上,让它查询数据库并在div标记中回显您要替换的更新HTML,然后就可以在success参数内的匿名函数中将div HTML替换为返回的HTML:

Javascript文件中的Ajax调用

//wrap up your data in a GET style string seperated by ampersands (&)
var dataString = foo=1234&bar=12345

$.ajax({
  type: "POST",
  url: ajax.php,
  data: dataString,
  success: function(html) {
    $("#refresh_div").html(html);
},
  dataType: dataType
});
然后,在“ajax.php”中,通过$\u POST superglobal访问刚刚发送到php脚本的变量

$foo = $_POST['foo'];
$bar = $_POST['bar']

$mysqli= new mysqli($params);
$stmt = $mysqli->prepare("SELECT * FROM myTable WHERE id=?");
$stmt->bind_param('i',$foo);
$stmt->execute();
$stmt->bind_result($result);
$stmt->fetch();
$stmt->close();

echo $result
所有回显的文本/html将作为html变量传递回ajaxsuccess参数,然后您可以用jquery替换divs内容


希望这有帮助。

你知道吗?在谷歌搜索中输入问题的标题field@hek2mgl如果你知道,为什么不给他一个链接呢?给一个人一条鱼,他总有一天会吃的,…)@hek2mgl谢谢你的建议,但我以前做过,运气不好。@Aemz告诉我你到底不明白什么,我会帮你的。请发布一些您已经尝试过的代码或教程。顺便说一句,我没有否决这篇文章