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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.