开发者

Jquery ajax posting wrong data

I have a a form:

<form id="deletesubmit" style="display:inline" >
    <input  style="width:50px" type="text" id="delcustomerid" name="delcustomerid" value="'.$row['customersid'].'">
    <button type="submit" class="table-butt开发者_JS百科on ui-state-default ui-corner-all" title="delete"><span class="ui-icon ui-icon-trash"></span></button>
</form>

The form gets the customers id and inserts it as value. It shows the correct customer is for that row everything is fine. Then when i post the form via ajax somehow it posts the id of a diffent row. This is the script:

$("form#deletesubmit").submit(function () {

    var delcustomerid = $('#delcustomerid').attr('value');
    $.ajax({
        type: "POST",
        url: "delete/process.php",
        data: "delcustomerid=" + delcustomerid,
        success: refreshTable
    });
    return false;
});
});

And finally here is the php to post the form:

<?php include("../../config/config.php"); ?>
<?php          
    $deleteid  = htmlspecialchars(trim($_POST['delcustomerid']));
    mysql_send("DELETE FROM customers where id='$deleteid'");
?>

I have tested it without the ajax and it works fine. There must be something missing. It is not posting the correct value. Spent days trying to work it out.


By using attr('value') you are pulling the original value which may not what you want; use .val() instead. Also, you can setup your call a little easier:

$("form#deletesubmit").submit(function() {
  var delcustomerid  = $(this).find('#delcustomerid').val();
  $.post( "delete/process.php", { delcustomerid: delcustomerid }, refreshTable );
  return false;
});

Finally, make sure that your PHP is outputting the right id by viewing the source of the generated HTML.

Also, in the PHP: Don't use htmlspecialchars to escape mysql. Since this supposed to be an integer, you can just use int:

$deleteid  = (int) trim($_POST['delcustomerid']);
mysql_send("DELETE FROM customers where id='$deleteid'");

Keep in mind, that an array (if someone passed deleteid[]= to this page) would evaluate to 1. So either build a test into it, or just make sure you don't have an id = 1 left in that table.


I was just going to suggest the .val() method. Also install firebug if you haven't already. It will allow you to see exactly what you are posting to the server.


Put an alert into the code prior to the $.ajax({ like so:

alert(delcustomerid);

Is the value wrong right before the post?

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜