簡體   English   中英

使用ajax保存“單擊以編輯”

[英]Save a “click to edit” with 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." ");
?>

我認為報價問題。 您可以使用帶有單引號的查詢,例如;

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM