使用ajax保存“单击以编辑”

时间:2013-12-22 14:49:13

标签: javascript php jquery 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." ");
?>

1 个答案:

答案 0 :(得分:0)

我认为引用问题。您可以使用单引号查询;

"mysql_query("UPDATE items SET Text ='$noutext' WHERE IDitem ='$IDitem'");