jQuery post function not working consistently (drops data)
I'm having a problem posting data using jquery. The function seems to be very buggy.
ie/ sometimes my form will post and the data will get stored while at other times it doesn't store at all...
HERE'S THE 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();
}
HERE'S THE 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!";
}
I guess it depends on the entries that you put in your form. It can not work one time and not next time. Maybe if you put a " or other unescaped sybmols it breaks the jquery post data.
You could use that $("#your_form_id").serialize();
just add postvar:1 and action:'createEntry' as hidden fields.
精彩评论