How to refresh a div content generated by the same php page using jquery
i have a test.php
, that contains a div called refreshdiv
, a button called refreshbutton
and may other div's that display other contents
the content of refresh开发者_运维知识库div
div is generated by php
is it possible to reload the contents of the refreshdiv
on clicking refreshbutton
on the same page ie, test.php
here is my work around
<div id="refreshdiv">
<table>
<?php
$rec=mysql_query("select * from user_master");
for($i=0;$i<mysql_fetch_array($rec);$i++)
{
?>
<tr>
<td>
<?php echo mysql_result($rec,$i,'username');?>
</td>
</tr>
<?php } ?>
</table>
</div>
tried using $.get, but didnt get any result
Take a look at this jsFiddle I put together - it may help.
I'm making an AJAX call (a POST in this case since it's just HTML and that's what jsFiddle supports for HTML requests - but it would be no different for a $.get
for you) that gets data and appends it to a table data cell (<td>
). The whole page doesn't update - just the section that I'm targeting -- in this case, the <td>
cell, which keeps having "hello's" appended into it.
I hope this helps. Let me know if you have add'l questions.
Use ajax
In the test.php use
if($_GET['ajax'] == 1) {
//echo new content;
}
and the jQuery code will be
function refreshClick() {
$("#refreshdiv").load("./test.php?ajax=1");
//OR
//to customize your call more, you could do
$.ajax({
method: "GET",
url: "./test.php?ajax=1",
success: function(data) { $("#refreshdiv").html(data); },
error: function(err){ Some_Error_Div.innerHTML = err; }
});
}
I think you'd need to set up a php page that will return the contents of the div then access this page via ajax and insert the contents generated by the page into your div. So the jQuery would look something like this -
$.ajax({
url: "div.php",
success: function(data){
$('#refreshdiv').html(data);
}
});
精彩评论