无法使用包含一些Php代码的jquery ajax重新加载div标记
我有一个表,其中的数据来自数据库。我在同一页上有一个表单,可以在表中插入数据。但是当我使用无法使用包含一些Php代码的jquery ajax重新加载div标记,php,jquery,html,ajax,Php,Jquery,Html,Ajax,我有一个表,其中的数据来自数据库。我在同一页上有一个表单,可以在表中插入数据。但是当我使用$插入数据时。ajax,它会在数据库中插入记录,但在相同的函数中重新加载表后不起作用,这意味着它不会重新加载表。 下面是我的代码 HTML <div class="col-lg-8" id="value_table"> <div class="panel-body"> <div class="table-responsive">
$插入数据时。ajax
,它会在数据库中插入记录,但在相同的函数中重新加载表后不起作用,这意味着它不会重新加载表。
下面是我的代码
HTML
<div class="col-lg-8" id="value_table">
<div class="panel-body">
<div class="table-responsive">
<?php
$q=$db->query('select attr_val_id,attr_val,sortorder,status from db_attribute_value where attr_id="'.$attr_id.'"' );
?>
<table cellpadding="0" cellspacing="0" border="0" class="table table-striped table-bordered" id="example">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Edit/Delete</th>
</tr>
</thead>
<tbody>
<?php $c=1; while($r=$q->fetch_assoc()){?>
<tr class=" gradeX">
<td><?php echo $c; ?></td>
<td><?php echo $r['attr_val']; ?></td>
<td><a href="add_attribute_val.php?id=<?php echo $r['attr_val_id']; ?>&attr_id=<?php echo @$r['attr_id']; ?>">
<button type="button" class="btn btn-default btn-circle"><i class="fa fa-pencil-square-o"></i></button>
</a>
<button type="button" class="btn btn-danger btn-default btn-circle" id="<?php echo $r['attr_val_id']; ?>" name="deleteRecord"><i class="fa fa-times"></i></button></td>
</tr>
<?php $c++;}?>
</tbody>
</table>
</div>
<!-- /.table-responsive -->
</div>
</div>
我猜您认为这行应该做的是从响应中获取数据,并在DOM中替换相同的标记:
$(“#值#表”).html($(“#值#表”).html()代码>
您应该使用类似的方式:
$(“#value_table”).html($.parseHTML(data.find(“#value_table”).html())代码>
此代码尚未测试,但主要思想是解析数据
参数并查找#value_表
元素
此处的更多信息:正如您所说,似乎“admin\u operation.php?mode=add\u attr\u value”未成功,请尝试调用always function,而不是success(用于调试它)。
如果always函数调用成功,则必须从admin_operation.php返回true
如果没有,则插入后admin_operation.php出现问题。重新加载哪个函数?什么意思是它不工作?这张表格在哪里?及。。。。。。。。。。。你能澄清一下吗?@echo_Me我正在用#value_表重新加载div标记。我不知道如何重新加载。所以我确实使用了.html()方法
$('#attr_val_form').submit(function(event) {
//$('.form-group').removeClass('has-error'); // remove the error class
//$('.help-block').remove(); // remove the error text
// get the form data
// there are many ways to get this data using jQuery (you can use the class or id also)
var formData = {
'attr_val' : $('input[name=attr_val]').val(),
'attr_id' : $('input[name=attr_id]').val(),
'sortorder' : $('input[name=sortorder]').val(),
};
// process the form
$.ajax({
type : 'POST', // define the type of HTTP verb we want to use (POST for our form)
url : 'admin_operation.php?mode=add_attr_value', // the url where we want to POST
data : formData, // our data object
cache: false,
success: function(data)
{
if($.trim(data)== 'yes')
{
$("#value_table").html($("#value_table").html());
$("#notice")
.show()
.html('<div class="alert alert-success"<strong>Successfully !</strong> record added.</div>')
.fadeOut(3000);
}
else
{
$("#notice")
.show()
.html('<div class="alert alert-danger"<strong>Error !</strong>Record already exist.</div>')
.fadeOut(10000);
}
}
})
// using the done promise callback
// stop the form from submitting the normal way and refreshing the page
event.preventDefault();
});
});