Jump to content

Passing data via Jquery to delete data from a mysql database


ridgey28

Recommended Posts

Hi I am trying to get data from selected checkboxes using jQuery $post and then getting the values using PHP to delete from a database.  I also would like a confirmation dialog to see if the user wants to delete the events.  I have looked at various methods on the web but I keep getting stuck.  I know a bit about jQuery but not a lot about ajax.  Any pointers will be appreciated.

 

Here is the code so far.

list.phpLists the events

<form name='delete_form' action='' method='post'>
<input type="checkbox" value="131" name="deleteCB[]" class="cb" />
<input type="checkbox" value="32" name="deleteCB[]" class="cb" />
<input type="checkbox" value="129" name="deleteCB[]" class="cb" />
<input type='submit' id='deleteBtn' value='Delete' name='DeleteBtn' />
</form>

 

list.js  Not sure if any of this is correct!

$(document).ready(function(){
      $("#deleteBtn").click(function() {
            var confirmation = confirm("Are You Sure You Want to Delete These Events?")
            if(confirmation == true)
    {
                    $(':checkbox:checked').each(function()
                    {
                        $.post('process.php', { deleteCB: $(this).attr('value') }, 
                        function(msg){$('.result').html(msg);
                                        alert('Items were successfully deleted.')});   
                    });
                    
                }
             else
                {
                    return false;
                }
    });
});

 

process.phpProcess the deletion of events

$value=$_POST['deleteCB'];


         $db->connect();
                $query_delete = "DELETE FROM events WHERE id='$value'";
                $db->query($query_delete);
                $db->close();


 

 

Thanks in advance

Link to comment
Share on other sites

Change the submit button to type="button" or if you want to use the submit button prevent the form from submitting when the submit button is clicked.

 

$('#delete_form').submit(function() {

        return false;

  }); 

 

In my example the process.php page just returns $_POST['deleteCB'];

 

Example:  http://www.realtown.com/test9.php

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.