JQuery .validate in a while loop

I am having a problem where I have forms in a while loop and I am trying to submit them using JQuery to delete the entry from mysql database. The problem is that when I press delete, it sends the same id no matter which delete button i press.

$("#deletePost").validate({
        debug: false,
        rules: {        
        },
        //If the rules are not met, the following messages are displayed beside the input field.
        messages: {
        },
        // do other stuff for a valid form
        submitHandler: function(form) {
            $.post('bin/Profile.php', $("#deletePost").serialize(), function(data) {
                $('#ProfileWall').prepend( data );
            });
        }
    });
}); 

            <div id='ProfileWall'>
            <?php 

                $WallRequest = mysql_query("SELECT * from WallPost where WallOf = '".$sn."' group by TimeAndDate DESC;")or die(mysql_error());
                while($WallInformation = mysql_fetch_array($WallRequest))
                {
                    $PostedBy =  mysql_fetch_array(mysql_query("SELECT * from members where ServiceNumber = '".$WallInformation['PostedBy']."';")); 
            ?>      

                        <div id = "post_<?php echo $WallInformation["id"]; ?>" >    
                        <form name = 'deletePost' id = 'deletePost' method = 'post'>
                        <table border = '0' width = '550' height = '60' valign = 'top'    align = 'center'>
                            <tr valign = 'center'>
                                <td width = '10' valign = 'center'>
                                    <input type = "hidden" name = "id" id = "id" value = "<?php echo $WallInformation['id']; ?>" >
                                    <input type = "hidden" name = "sn" id = "sn" value = "<?php echo $sn; ?>" >
                                    <input type = "submit" name="Delete" id = "Delete" value="Delete" class="delete" />
                                </td>
                            </tr>
                        </table>
                        </form>
                        </div>

          <?php } ?>
        </div>

Answers


The problem is that you're always serializing the same form in your submitHandler. Instead of selecting by ID, like $('#deletePost'), you could consider giving each entry a unique id with the same class in the markup (<form id="$generate_a_unique_id_and_put_it_here" class="deletePost">), then using $(this) to select only the instance of .deletePost you want to submit in submitHandler.


Need Your Help

Dynamic Button Click in Table

asp.net vb.net button dynamic

Creating a dynamic button and linking it to the same event for all buttons created.

Rspec Capybara get port before run first visit method

ruby-on-rails selenium rspec capybara

I'm try to run Capybara server before all tests to get server port which I should include in url like 'http://subdomain.domain.au:CURRENT_PORT_NUMBER/'