使用包含PHP内容的JavaScript重新加载div
我有一个PHP页面,其结构如下:使用包含PHP内容的JavaScript重新加载div,php,javascript,Php,Javascript,我有一个PHP页面,其结构如下: <?php include 'header.php'; include 'content.php'; include 'footer.php'; ?> 那很好。现在,在content.php文件中,用户可以执行更改标题中的$show值的操作,但我需要重新加载页面以查看更新的$show编号 我想用JavaScript解决这个问题,但不知道怎么做 我尝试用JavaScript重新加载计时器来解决它,如下所示: <script> functi
<?php
include 'header.php';
include 'content.php';
include 'footer.php';
?>
那很好。现在,在content.php文件中,用户可以执行更改标题中的$show
值的操作,但我需要重新加载页面以查看更新的$show
编号
我想用JavaScript解决这个问题,但不知道怎么做
我尝试用JavaScript重新加载计时器来解决它,如下所示:
<script>
function render (){
$('.customer-database').html(.customer-database)
}
window.setInterval(render, 500);
</script>
函数渲染(){
$('.customer database').html(.customer database)
}
设置间隔(渲染,500);
$show
计数器位于div class客户数据库中。这不起作用,因为我需要在HTML之后放入HTML代码,但我不想放入HTML代码。我只是想重新加载它。这可能吗
我愿意接受JavaScript和PHP中的任何建议。您可以做的是对一个单独的PHP文件进行AJAX请求。单独的PHP文件包含返回与您的代码类似的数字的代码:
$show =$mysql->count('someparam', $foo['bar']);
if ($show > 0) {
echo $show;
}
使用jQuery创建AJAX调用时,请参阅以获取帮助。您可以做的是对一个单独的PHP文件发出AJAX请求。单独的PHP文件包含返回与您的代码类似的数字的代码:
$show =$mysql->count('someparam', $foo['bar']);
if ($show > 0) {
echo $show;
}
使用jQuery创建AJAX调用时,请参阅以获取帮助。如果您知道要替换的内容区域的名称/id,则插入jQuery AJAX请求以仅重新加载页面的一部分相当简单。假设已经包含了jquery库,那么就可以使用这个javascript块
path = "/path/to/my/action/"
$.ajax({
url : path,
success : function(response) {
$('.customer-database').replaceWith(response);
},
error : function(xhr) {
alert('Error! Status = ' + xhr.status);
}
});
然后,您可以编写一个基本上只给您计数的操作,并将该操作重新加载到元素中-该操作以您喜欢的任何计数+html响应如果您知道要替换的内容区域的名称/id是什么,那么插入jquery ajax请求仅重新加载页面的一部分是相当简单的。假设已经包含了jquery库,那么就可以使用这个javascript块
path = "/path/to/my/action/"
$.ajax({
url : path,
success : function(response) {
$('.customer-database').replaceWith(response);
},
error : function(xhr) {
alert('Error! Status = ' + xhr.status);
}
});
然后,您可以编写一个动作,基本上只给出您的计数,然后将该动作重新加载到元素中-该动作以您喜欢的任何计数+html进行响应
function render (){
$('.customer-database').parent().load('myscript.php .customer-database');
^ ^ ^ ^ ^ ^ ^ ^
| select the container in | | | url | | select content | |
| which to reload content | | | to your | | to put in the | |
|______________________________| | | page | | container | |
| |__________| |________________| |
| send ajax call and replace content |
|_____________________________________|
}
这应该起作用:
function render (){
$('.customer-database').parent().load('myscript.php .customer-database');
^ ^ ^ ^ ^ ^ ^ ^
| select the container in | | | url | | select content | |
| which to reload content | | | to your | | to put in the | |
|______________________________| | | page | | container | |
| |__________| |________________| |
| send ajax call and replace content |
|_____________________________________|
}
正如其他人所描述的,您希望使用ajax。jQuery在这方面非常擅长。使用jQuery,它将与
//whatever you do to change the $show value as a trigger, maybe a click?
$(".show_value_button").click(function(){
$("#header_div_id").load("header.php");
});
正如其他人所描述的,您希望使用ajax。jQuery在这方面非常擅长。使用jQuery,它将与
//whatever you do to change the $show value as a trigger, maybe a click?
$(".show_value_button").click(function(){
$("#header_div_id").load("header.php");
});
你不能这样做。必须在服务器端处理php文件。您可以使用的是ajax
:您(用javascript)调用一个URL,该URL将返回经过处理的html代码和更新的值。我明白了。。假设我将计数器部分更改为AJAX,如何用javascript重新加载div?你不能这样做。必须在服务器端处理php文件。您可以使用的是ajax
:您(用javascript)调用一个URL,该URL将返回经过处理的html代码和更新的值。我明白了。。假设我将计数器部分更改为AJAX,我将如何用javascript重新加载div?