Jump to content

deleting the records selected using checkbox


Vidya_tr

Recommended Posts

I have a list of record displayed and a checkbox against each of them.

I need to select the checkboxes to delete the records.After selecting ,on clicking the DELETE button the records should be deleted from database and should be cleared from the list.

Can i do this with php and javascript alone. or Is it possible only with Ajax???

 

How can I retrive the values of the checked checkboxes and delete the records? Pls help me.

I googled a lot .But not able to get a clear idea.I am new to php and not familiar with Ajax.

Please help me to find the solution...

Link to comment
Share on other sites

You can do it with just PHP, by having it as a form and posting it to a processing page which deletes all records WHERE the id is in the array of checkboxes. AJAX is just for making it cool so the page doesn't need to reload. There isn't any real point of it in this case.

 

Basically, you have a list of checkboxes:

 

<input type="checkbox" name="deleteme[]" value="1"> Check 1<br />
<input type="checkbox" name="deleteme[]" value="2"> Check 3<br />
<input type="checkbox" name="deleteme[]" value="3"> Check 3<br />

 

Notice how we have created an array of the checkboxes. So when you post and do this:

 

print_r($_POST['deleteme']);

 

All of the selected checkbox values will be in an array. So to use it in a query you use implode() to turn it into a string seperated by commas then delete.

 

mysql_query("DELETE FROM records WHERE id IN('" . implode(",", $_POST['deleteme']) . "')") or die("Error: ".mysql_error());

 

Hope that makes some sense.

Link to comment
Share on other sites

thanks for the help...

But in the solution you provided ,is it necessary that I should have the name of the checkbox in the database since you use it in the query.

 

My page that displays the records and the checkboxes are created dynamically.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.