在每一行中,我想添加一个可编辑的td来插入" mavandon"进入DATABASE dsdonhang。我在http://phppot.com/php/php-mysql-inline-editing-using-jquery-ajax/上看了这个,但它不起作用:((
<script src="../lib/jquery-3.1.0.min.js"></script>
<script>
$(document).ready(function(){
function saveToDatabase(editableObj,column,idd) {
$.ajax({
url: "saveedit.php",
type: "POST",
data:'column='+$(this).column+'&editval='+$(this).editableObj.innerHTML+'&idd='+$(this).idd,
success: function(data){
$(editableObj).css("background","#FDFDFD");
//alert ("hello");
}
error: function() {}
});
}
});
</script>
<?php $query = mysqli_query($conn,"SELECT * FROM dsdonhang");
while($row=mysqli_fetch_assoc($query)) {$data[] = $row;}
foreach($data as $k=>$v) {
?>
<tr>
<td><?php $madon = $data[$k]["idd"];echo $k+1; ?></td>
<td><?php echo $data[$k]["ngaydat"]; ?></td>
<td><?php echo $data[$k]["hoten"]; ?></td>
<td><?php echo $data[$k]["diachi"]; ?></td>
<td><?php echo $data[$k]["sdt1"]; ?></td>
<td><?php echo $data[$k]["donhang"]; ?></td>
<td><?php echo $data[$k]["tongtien"]; ?>.000VNĐ</td>
<td><?php echo $data[$k]["nguoinhan"]; ?></td>
<td contenteditable="true" onchange="saveToDatabase(this,'mavandon','<?php echo $madon; ?>')"><?php echo $data[$k]["mavandon"]; ?></td>
<td><a href="donhang.php?id=<?php echo $data[$k]["idd"]; ?>&tc=<?php echo $data[$k]["sothamchieu"]; ?>">Xem</a></td>
</tr>
<?php
}
?>
saveedit.php
<?php
require_once("../lib/connection.php");
mysqli_query($conn,"UPDATE dsdonhang set " . $_POST["column"] . " = '".$_POST["editval"]."' WHERE idd='".$_POST["idd"]."'");
?>
有任何帮助吗?感谢
答案 0 :(得分:0)
使用onblur
代替onchange
<td contenteditable="true" onblur="saveToDatabase(this,'mavandon','<?php echo $madon; ?>')" onClick="showEdit(this);"><?php echo $data[$k]["mavandon"]; ?></td>
答案 1 :(得分:0)
在ajax调用中传递params时错误/错误。按照下面的说明进行修改
add this in ajax call. '&idd='+idd Instead of this '&idd='+$(this).idd
答案 2 :(得分:0)
试试这个:
<script>
$(document).ready(function(){
function saveToDatabase(editableObj,column,idd) {
var data = {'column':$(this).column,'editval':$(editableObj).text(),'idd':$(this).idd };
$.ajax({
url: "saveedit.php",
type: "POST",
contentType: "application/json; charset=utf-8",
datatType: "json",
data:JSON.stringify(data),
success: function(data){
$(editableObj).css("background","#FDFDFD");
console.log("received data=>"+data);
alert ("hello");
}
error: function(err) {
console.log("error=>"+err); //print error if exist
}
});
}
});
</script>
答案 3 :(得分:0)
确保调用你的事件。(你也可以查看firebug。)
将参数传递为
{column:$(this).column,editval:$(this).editableObj.innerHTML,idd:$(this).idd}