[英]Posting inline-table edit to sql
我有一张要向其中添加内联编辑的表格,但是我对ajax真的很陌生。 我怀疑我的Ajax代码的dataString
,但我找不到。 代码本身可以工作,但是我在发布数据时遇到了麻烦。
很抱歉,如果以前曾问过这种问题,我真的很在意。 如果有人可以告诉我该怎么做,我将不胜感激。
数据库
rowid | Person
4 | mike
12 | peterson
的PHP
//Every row in the sql table has an row id
<php $id = $row['rowid']; ?>
<tr id="<?php echo $id ?>" class="tredit>
<td>
<span id="person_<?php echo $id ?>" class="text"><?php echo $row["Person"] ?></span>
<input type="text" class="ip" id="person_ip_<?php echo $id ?>" value="<?php echo $row["Person"] ?>">
</td>
</tr>
阿贾克斯
$(document).ready(function(){
$(".tredit").click(function(){
var ID=$(this).attr('id');
$("#person_"+ID).hide();
$("#person_ip_"+ID).show();
}).change(function(){
var ID=$(this).attr('id');
var first=$("#person_ip_"+ID).val();
var dataString = 'id='+ ID +'&person='+first;
//alert(dataString);
$("#person_"+ID).html('<img src="load.gif" />');
if(first.length > 0){
$.ajax({
type: "POST",
url: "post_table.php",
data: dataString,
cache: false,
success: function(html){
$("#klant_"+ID).html(first);
}
});
}else{
alert('Voer iets in');
}
});
});
post_table.php
<?php
$person= $_POST['person'];
$id = $_POST['id'];
//This is where I get stuck, I don't know how to extract data from the dataString to insert into an update
$query = "update people set Person='$person' where id='$id'";
mysql_query($query, $con);
?>
关于检索在post()函数中指定的数据,请查看jquery官方文档:
https://api.jquery.com/jquery.post/
您会看到有一种可用的格式,该格式允许您为params指定名称:
{ name: "John", time: "2pm" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.