0
我正在制作一个简单的列表应用程序,并且这些项目应该易于编辑(只需单击它们)。它工作正常,但我仍然有问题保存到数据库的更改,出于某种原因它不会发生。我确定这是一个代码问题,但我一直无法找到它。使用ajax保存“点击编辑”
列表页
<input type="hidden" id="hidden" id="hidden" value="">
<ul id="lista">
<?php
$IDllista = 1;
$selectitems = mysql_query ("SELECT * FROM items WHERE IDllista=".$IDllista." ORDER BY posicio ASC") or die (mysql_error());
while ($row = mysql_fetch_array($selectitems))
{
echo'<li class="item" id="';
echo $row['IDitem'];
echo '"><span class="edit" id="span-';
echo $row['IDitem'];
echo '" data="';
echo $row['Text'];
echo '">';
echo $row['Text'];
echo'</span></li>';
}
?>
</ul>
的JavaScript
//Call the edit function
$(".edit").live("click", function() {
// Get the id name
var iditem = $(this).parent().attr("id");
var element = this;
var id = element.id;
//New text box with id and name according to position
var textboxs = '<input type="text" name="text' + id + '" id="text' + id + '" class="textbox" >'
//Place textbox in page to enter value
$('#'+id).html('').html(textboxs);
//Set value of hidden field
$('#hidden').val(id);
//Focus the newly created textbox
$('#text'+id).focus();
});
// Even to save the data - When user clicks out side of textbox
$('.edit').focusout(function() {
//Get the value of hidden field (Currently editing field)
var field = $('#hidden').val();
//get the value on text box (New value entred by user)
var value = $('#text'+field).val();
//Update if the value in not empty
if(value != '') {
//Post to a php file - To update at backend
//Set the data attribue with new value
$(this).html(value).attr('data', value);
$.ajax({
type: 'POST',
data: {id: iditem},
url: 'editartext.php'
});
}
// If user exits without making any changes
$(".edit").each(function() {
//set the default value
$(this).text($(this).attr('data'));
});
});
编辑页面
<?php
include_once "../connect.php";
$IDitem = $_POST['id'];
$noutext = "hola";
$actualitza = mysql_query ("UPDATE items SET Text = ".$noutext." WHERE IDitem = ".$IDitem." ");
?>
'$ noutext'是一个字符串,所以应该引用它,如果'$ IDitem'是一个字符串,它也应该被引用,它也应该被消毒。 – Musa
jQuery.live()从版本1.7开始已被弃用,并在1.9中被删除。您可能仍然使用旧版本,但它是更新和使用API的当前受支持部分的理想选择 – Deryck
我无法确定您的代码出了什么问题,但您应该在服务器端打印某些内容以检查“id”并检查查询的结果。 –