Page 1 of 1

PHP/Jquery/MySQL Delete a row

Posted: Wed Feb 04, 2009 11:16 pm
by grandroyal
So i have a page that displays groups with delete links that use Jquery to determine ID and then submit a PHP script through AJAX for the PHP delete function and then refresh the page. But all it does is refresh. I know its grabbing the variable containing the ID correctly through testing but I'm not sure whats wrong from there.

Thanks in advance for the help!

Originating page

Code: Select all

<?php include("includes/header_nav.php"); ?>
    
    <div class="contwrapper">
    <div class="textheader">Groups</div>
    <div style="float:left; width:500px;">
 
<?php include("includes/db_connect.php"); ?>
 
<?php
   //display all the news
     $result = mysql_query("select * from groups");
    echo "<ul>\n";
    while($row = mysql_fetch_object($result))
    {
    echo "<li>$row->groupname <span><a href='#' class='delete-this'>Delete</a><input type='hidden' name='pid' value='$row->groupID'></span></li>\n";
    }
    echo "</ul>\n";
?>
<script type="text/javascript">
 $(document).ready(function(){
   $("a.delete-this").click(function(){
        var groupid = $(this).next('input').val();
                $.ajax({
                        type: "POST",
                        url: "includes/delete_group.php",
                        data: "pid=" + groupid,
                        success: window.location="groups.php"
                        });
        
    });
});
</script>
 
delete_group.php

Code: Select all

<?php
 
    $con = mysql_connect("localhost","xxx","xxx");
    if (!$con)
      {
      die('Could not connect: ' . mysql_error());
      }
    mysql_select_db("mkeartco_castiron", $con);
    
    $groupid = $_POST['pid'];
 
    $sql = "DELETE FROM groups WHERE groupID='$groupid'";             
    $result = mysql_query($sql) or die("Unable to query:".mysql_error());
 
 
    mysql_close($con);
?>
Edit: Removed parenthesis around $_POST['pid']

Re: PHP/Jquery/MySQL Delete a row

Posted: Thu Feb 05, 2009 3:41 am
by mickeyunderscore
It's good that you are using AJAX with jQuery as I love both! However, if you are refreshing the whole page after the request, then you are kind of defeating the purpose of AJAX.

Anyway, are you are absolutely sure it is picking up the pid? Have you tried window.alert(groupid) before the AJAX call?

AJAX can be difficult to debug if it is the server side that is causing the issue. I tend to use Firebug for Firefox which allows me to view headers sent and the servers response, e.g. so I can see any errors that are being caused.

I think your main problem here is that you can't see what the remote script is doing, try running the remote script by itself, change the $_POST to a $_GET and pass a variable in manually.

Also, if you are simply posting, you may find it better and easier to use the jQuery post method, this is what I tend to use.

Re: PHP/Jquery/MySQL Delete a row

Posted: Thu Feb 05, 2009 9:11 am
by grandroyal
So I have it alert the groupid before the ajax call and that works fine, and i also used the delete_groups.php on its own and it works fine too.

The only thing I can think of is the ajax call messing things up but the page refreshes meaning it was successful right? I can try to use jquery's post instead but all of the documentation I found about it works only on form submit not click function. Can you show me what it would like like with my parameters?

Thanks

Re: PHP/Jquery/MySQL Delete a row

Posted: Thu Feb 05, 2009 1:28 pm
by mickeyunderscore
Pretty much any JavaScript can be bound to a click event. Change your form 'submit' to a 'button' instead, then bind the event:

Code: Select all

$('#formButton').click( function() {
        $.post('includes/delete_group.php', {pid:groupid}, function() {
            window.location="groups.php"
        })
    })
Something that might be useful to check whether the script is outputting any kind of errors:

Code: Select all

$('#formButton').click( function() {
        $.post('includes/delete_group.php', {pid:groupid}, function(return) {
            window.alert(return);
        })
    })

Re: PHP/Jquery/MySQL Delete a row

Posted: Thu Feb 05, 2009 3:29 pm
by grandroyal
Thank you so much , that did it.