views:

468

answers:

4

I'm generating an html file which looks like:

<tr id="ID001" property1="PROPERTY001"><td><input type="checkbox" 
       name="row_checkbox_ID001"></td><td>...</td><td>...</td></tr>
<tr id="ID002" property1="PROPERTY002"><td><input type="checkbox" 
       name="row_checkbox_ID002"></td><td>...</td><td>...</td></tr>

When the user selects individual rows for deletion, how can I (through jQuery), pass this to a php script?

I will need to build something like this:

$(document).ready(function () {
    $('#contact-form input.contact-delete').click(function (e) {
        e.preventDefault();
        $.get("deleterecord.php", ...);
    });
});

There can be 0, 1 or multiple rows... The HTML being generated is under my control and can be modified.


Clarification:

I want to have a button above all these rows which the user can click on AFTER he has selected the rows from the table.

<div id='contact-form'><h2>Contacts</h2>
<input type='button' name='contact-delete' value='Delete Record(s)' 
class='contact-delete'/>

The TRs need to be deleted, but BEFORE that, the deleterecord.php script needs to be called with the TR ids.

+1  A: 

Use HTML arrays

<input type="checkbox" name="chk[]" value="01234">
<input type="checkbox" name="chk[]" value="98765">
Pentium10
Hi Pentium10 - that's cool, but how do I pass the "checked" checkboxs' IDs to the php script?
Steve
If you issue a submit for the form, they will pass to php and you will receive an array for them such as `print_r($_POST[chk]);`
Pentium10
A: 

You could create a JSON object containing details of any rows selected (ID's or whatever you need) and send that to your php script. Check out this article on json, php + jquery.

Fermin
A: 

I believe, you are looking for similar behavior:

$('#contact-form input.contact-delete').click(function (e) {
    var $this = $(this);
    // find the table row, in which are elements contained
    var $tr = $this.closest('tr');
    // take id
    var id = $tr.attr('id');
    // ajax with id
    $.get("deleterecord.php?id="+id, function (data) {
        // remove table row on success
        $tr.remove();
    });
    e.preventDefault();
});
Juraj Blahunka
Juraj - Thanks for replying. Have a look at my clarification above. The click() function is for a button that should see which TR ids have been selected and pass them to the php script...
Steve
A: 

Hello peeps!

Sorry for answering my own question, but the SO post at: http://stackoverflow.com/questions/590018/getting-all-selected-checkboxes-in-an-array neatly solves my problem!

For future visitors to this page, pay attention to variable names in your HTML and how you're accessing the input checkboxes in jQuery!

Steve