views:

59

answers:

3

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.

A: 

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

alert(delcustomerid);

Is the value wrong right before the post?

C Bauer
added alert(delcustomerid); and yes the value is wrong before post. Does that mean it is a problem with the form??
+3  A: 

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.

Doug Neiner
In addition, I do not believe that the way that "data" is formatted will function correctly. The way that Doug has it formatted is the correct way. { delcustomerid: delcustomerid }
Buggabill
Thanks for the response. Added your code and the problem stills persists. Added alert(delcustomerid); prior to $.ajax as posted below and the wrong value is set before post.
Haha, I think I might know what's happening. Try my update (just the JS, I didn't change the PHP). Are you using multiple forms like this on single page?
Doug Neiner
Yes I have two forms
YOU ARE THE MAN!!!! YOU ARE THE MAN!!!! YOU ARE THE MAN!!!! It works, It works, It works
Thankyou so much. I cannot tell you how long I have been looking at that page.
I can sleep at ease tonight
@user272899 Haha... glad it works! Since you used the same id twice on a page, it would always pull back the first one, not the one you wanted. You can avoid that in the future with using classes and that `find` trick I used. Good luck on the rest of your project!
Doug Neiner
@user272899 Did you mean to accept this answer as correct?
Doug Neiner
I'm confused. If @Doug provided the correct answer, why wasn't his solution accepted?
Jonathan Sampson
Ticked the wrong box apologies Doug. Once again You the man!
+2  A: 

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.

Brian
+1 for mentioning Firebug. Great tool!
Doug Neiner