Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2008
    Location
    Aberdeenshire
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question Passing data via Jquery to delete data from a mysql database

    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.

    list.php
    Code:
    <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>
    lit.js Not sure if this is correct
    Code:
    $(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.php
    PHP Code:
    $value=$_POST['deleteCB'];


             
    $db->connect();
                    
    $query_delete "DELETE FROM events WHERE id='$value'";
                    
    $db->query($query_delete);
                    
    $db->close(); 
    Thanks in advance
    Follow me on Twitter @ridgey28

  • #2
    Senior Coder Spudhead's Avatar
    Join Date
    Jun 2002
    Location
    London, UK
    Posts
    1,856
    Thanks
    8
    Thanked 110 Times in 109 Posts
    You might find this page useful.

    Also: validate your PHP input. What if some malicious person POSTed "' OR 1=1--" ?

  • Users who have thanked Spudhead for this post:

    CoolAsCarlito (12-05-2010)

  • #3
    New Coder
    Join Date
    Jul 2008
    Location
    Aberdeenshire
    Posts
    16
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks that site gave me some good pointers. I have included validation in my script I just didn't add it to my thread.
    Follow me on Twitter @ridgey28

  • #4
    Regular Coder
    Join Date
    Jun 2008
    Posts
    682
    Thanks
    114
    Thanked 2 Times in 2 Posts
    I have a similiar use for this however my situation is that I have a listing of entries with checkboxes with their IDs attached to them in the coding but I have a dropdown with edit and delete and then a submit button next to it what kind of change would i make bases on the user's choice.

    Both of these code blocks are from the same page.
    Code:
    <div class="listActions">
    			<form action="" method="post">
    				<label for="actionSelect">With selected items: </label>
    				<select class="select" name="actionSelect" id="actionSelect">
    					<option id="1">Edit</option>
    					<option id="2">Delete</option>
    				</select>
    				<button class="button small-button"><strong>Apply</strong></button>
    			</form>
    		</div>
    PHP Code:
    <?php 
                
    while ( $row mysqli_fetch_array $resultMYSQL_ASSOC ) ) {
                  echo 
    '
                  <tr>
                  <td><input type=checkbox class=checkbox value="' 
    $row['id'] . '" /></td>
                  <td>' 
    $row['divisionname'] . '</td>
                  <td>' 
    $row['name'] . '</td>
                  <td class=last>' 
    $row['datecreated'] . '</td>
                  </tr>'
    ;
                }
            
    ?>
    Last edited by CoolAsCarlito; 12-05-2010 at 05:29 PM.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •