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-button 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:Finally, make sure that your PHP is outputting the right
idby viewing the source of the generated HTML.Also, in the PHP: Don’t use
htmlspecialcharsto escape mysql. Since this supposed to be an integer, you can just useint:Keep in mind, that an array (if someone passed
deleteid[]=to this page) would evaluate to1. So either build a test into it, or just make sure you don’t have anid= 1 left in that table.