Php 如何使用jquery刷新表
我在网上搜索我的问题,找到了最接近我的问题。 但我仍然无法找到我的问题的解决方案。我的问题是我有下面的代码Php 如何使用jquery刷新表,php,jquery,Php,Jquery,我在网上搜索我的问题,找到了最接近我的问题。 但我仍然无法找到我的问题的解决方案。我的问题是我有下面的代码 <h1>Time</h1> <input type="text" value="0.0" id="time"> <button id="FormSubmit">Submit</button> <img src="loading.gif" id="LoadingImage" style="d
<h1>Time</h1>
<input type="text" value="0.0" id="time">
<button id="FormSubmit">Submit</button>
<img src="loading.gif" id="LoadingImage" style="display:none" />
<table id="responds" >
<tr>
<th>Time</th>
</tr>
<?php
$results = $mysqli->query("SELECT * FROM time ORDER BY time");
//get all records from add_delete_record table
while($array = $results->fetch_assoc()) {
?>
<tr id="item_<?php echo $array["id"] ?>">
<td><?php echo $array['time']?>
<a href="#" class="del_button" id="del-<?php echo $array["id"] ?>">
<img src="icon_del.gif" />
</a>
</td>
</tr>
<?php }
?>
</table>
如果将表的内容保存在单独的php文件中:
// tablebody.php
//
<tr>
<th>Time</th>
</tr>
<?php
$results = $mysqli->query("SELECT * FROM time ORDER BY time");
//get all records from add_delete_record table
while($array = $results->fetch_assoc()) {
?>
<tr id="item_<?php echo $array["id"] ?>">
<td><?php echo $array['time']?>
<a href="#" class="del_button" id="del-<?php echo $array["id"] ?>">
<img src="icon_del.gif" />
</a>
</td>
</tr>
<?php }
?>
//tablebody.php
//
时间
然后在html文件中,您可以使用jquery将该文件加载到表中:
<table id="responds">
</table>
<script>
$(document).ready(function() {
$("#responds").load("tablebody.php");
});
</script>
// Append the following to time.php
$data = array();
$results = $mysqli->query("SELECT * FROM time ORDER BY time");
//get all records from add_delete_record table
while($array = $results->fetch_assoc()) {
$data[] = array('id' => $array['id'], 'time' => $array['time']);
}
echo json_encode( $data );
$(文档).ready(函数(){
$(“#responses”).load(“tablebody.php”);
});
为了有效地使用此方法,您可能需要稍微重新构造代码,但它会解决您的问题。您可以使用AJAX更新html表的结果,如下所示:
$.ajax({
type: "POST",
url: "controller.php", //php controller fecthing the info
data: {
param1: p1,
param2: p2 // controller params
},
success: function(data) {
if (data != null){
$("#yourTableID").html(data); //update table html
}else{
alert('No results found!');
}
}
});
您可以返回make the
time.php
,以返回包含表中项目集合的JSON:
<table id="responds">
</table>
<script>
$(document).ready(function() {
$("#responds").load("tablebody.php");
});
</script>
// Append the following to time.php
$data = array();
$results = $mysqli->query("SELECT * FROM time ORDER BY time");
//get all records from add_delete_record table
while($array = $results->fetch_assoc()) {
$data[] = array('id' => $array['id'], 'time' => $array['time']);
}
echo json_encode( $data );
然后,调用此脚本:
jQuery.ajax({
type: "POST",
url: "time.php",
dataType:"json", // INSTEAD OF text
data: {
content_txt: $("#time").val()
},
success:function(response) {
var $table = $("#responds").html('<tr><th>Time</th></tr>'); // RESET THE TABLE
for( var i = 0; i < response.length; i++) {
// Append the new rows to the table, based on the data returned from the server
$table.append('<tr id="item_' + response[i].id + '>' +
'<td>' + response[i].time + '>' +
'<a href="#" class="del_button" id="del-' + response[i].id + '>' +
'<img src="icon_del.gif" />' +
'</a>' +
'</td>' +
'</tr>');
}
// The following lines are commented-out because this runs in the background and only update the content of the table
// $("#contentText").val('');
// $("#FormSubmit").show();
// $("#LoadingImage").hide();
},
error:function (xhr, ajaxOptions, thrownError){
$("#FormSubmit").show();
$("#LoadingImage").hide();
alert(thrownError);
}
});
您可以使用AJAX来显示表。使用此解决方案,您可以设置如何运行AJAX。如果您想使用相同的php脚本重新加载表,您必须使用该链接中指出的AJAX。您是否查看了表部分,或者需要将表部分放在单独的文件中(
table.php
)m然后,您需要包含table.php代码>在根文档上使用PHP。接下来,在url参数中使用并调用table.php
脚本和更新的HTMLook,就像我必须更新问题一样,我无法将表保存在其他php文件中,您可以将其存储在字符串中并使用eval()吗?等等,我正在更新问题啊,看起来一切都很好,但是我如何每30秒更新一次这个表呢?表被重新加载了,但是没有内容我在这行for(var I=0;I
因为我警告response.length,但表内容未显示,而页面源代码正在显示内容。我无法相信这一点。HTML有内容但未显示