jQuery post函数不能正常工作(丢弃数据)

时间:2011-03-04 10:29:22

标签: jquery ajax

我在使用jquery发布数据时遇到问题。这个功能似乎非常错误。

即/有时我的表格会发布,数据会被存储,而有时则不会存储......

这是JS:

function createEntry(){

    item_title = $('#item_title').val();
    item_description = $('#item_description').val();
    item_category = $('#item_category').val();

    $('#status').html("<img src='images/ajax-loader.gif' border=0> Adding Entry").show();

    $.post("crud.php",{postvar:1, action:'createEntry', item_title:item_title, item_description:item_description, item_category:item_category}, function(html) { 
        $("#status").empty();    
        $("#status").append(html).fadeOut(2000);
        getAddedEntry();

    });

    $('#entry_textbox').clear();

}

这是PHP:

 if ($_POST['action'] == 'createEntry') {

    $title = mysql_real_escape_string($_POST['item_title']);
    $description = mysql_real_escape_string($_POST['item_description']);
    $category = mysql_real_escape_string($_POST['item_category']);

    $entry = "INSERT INTO items (title, description, category) VALUES ('$title','$description','$category')";  

    if (!mysql_query($entry,$conn)){
        die('Error: ' . mysql_error());
    }

    else {
        mysql_close($conn);
        echo "Entry Added!";
    }

1 个答案:

答案 0 :(得分:0)

我想这取决于你在表单中输入的条目。它不能一次工作,也不能下次工作。也许如果你把一个“或其他未转义的sybmols打破jquery发布数据。

您可以使用$("#your_form_id").serialize();只添加postvar:1和action:'createEntry'作为隐藏字段。