mdmartiny Posted October 23, 2013 Share Posted October 23, 2013 Hello Everyone, I am trying to send an id to a second page to delete a row from a mysql table. When the button is clicked it shows on the page that the row has been deleted but when the page is refreshed. The item that was supposed to be deleted has returned and it is still in the database. I made a JSfiddle for this http://jsfiddle.net/mikedmartiny/2DKNh/ I would appreciate any help Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/ Share on other sites More sharing options...
gristoi Posted October 23, 2013 Share Posted October 23, 2013 try: $.ajax( { type: "POST", url: "delpage.php", data: { id: id }, cache: false, success: function() { parent.fadeOut('slow', function() {$(this).remove();}); } }); Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455064 Share on other sites More sharing options...
mdmartiny Posted October 23, 2013 Author Share Posted October 23, 2013 It still does the same thing. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455065 Share on other sites More sharing options...
gristoi Posted October 23, 2013 Share Posted October 23, 2013 (edited) as a test, what happens if you go to delpage.php?id=1 ( make sure this is a real row id in the database) ? you need to make sure that the php isnt crapping out on the deletion. because its ajax ou could be getting an error back, but the success method triggers. giving you a false-positive, Also, have you looked at the response being sent back from the server in your browser console? Edited October 23, 2013 by gristoi Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455072 Share on other sites More sharing options...
mdmartiny Posted October 23, 2013 Author Share Posted October 23, 2013 I get no error messages in my console when I run the script. I actually went into the script and manually put in the id of the item I am trying to delete and it worked. Then when I try and do it dynamically it did not and keeps on doing what it has been doing all along. Here is the code that I am using for the delete page <?php require_once("../../includes/config.php"); $sql = mysqli_query($db, "SELECT `sender`, `reciever` FROM `conversations` WHERE `conversationID` = '". (int)$_POST['id']."'"); $check_sender_reciever = mysqli_fetch_assoc($sql); if ($check_sender_reciever['sender'] == $session_user_id) { $delete = mysqli_query($db,"UPDATE `conversations` SET `sdelete` = '1' WHERE (`conversationID` = '". (int)$_POST['id']."' OR parent='". (int)$_POST['id']."')"); } else if ($check_sender_reciever['reciever'] == $session_user_id) { $delete = mysqli_query($db,"UPDATE `conversations` SET `rdelete` = '1' WHERE (`conversationID` = '". (int)$_POST['id']."' OR parent='". (int)$_POST['id']."')"); } if ($delete === true) { $_SESSION['success'] = 'Message has been successfully deleted!'; } else { $_SESSION['error'] = "Error description: " . mysqli_error($db); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455120 Share on other sites More sharing options...
Ch0cu3r Posted October 23, 2013 Share Posted October 23, 2013 (edited) You need to use mysqli_affected_rows to check if the query did actually update the table. if ($delete === true) { if (mysqli_affected_rows($db)) { $_SESSION['success'] = 'Message has been successfully deleted!'; } else { // record was not updated } }else { $_SESSION['error'] = "Error description: " . mysqli_error($db); } Edited October 23, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455125 Share on other sites More sharing options...
mdmartiny Posted October 24, 2013 Author Share Posted October 24, 2013 I will change that but when I look in the database it is still there. So I know that it is not deleting it the way that it should. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455165 Share on other sites More sharing options...
Ch0cu3r Posted October 24, 2013 Share Posted October 24, 2013 looking at your javascript code at jsfiddle. This line var id = $(this).parent().parent().attr('id'); cant find the id value. The id value you need is from the delete button right? <input type='submit' id='1' class='delete' value='Delete'> If is then you need to use $(this).attr('id') to get the id value from the delete button. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455193 Share on other sites More sharing options...
mdmartiny Posted October 24, 2013 Author Share Posted October 24, 2013 Yes that is where the Id is coming from.... I changed it to what you said and it still is not deleting the field Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455196 Share on other sites More sharing options...
Ch0cu3r Posted October 24, 2013 Share Posted October 24, 2013 (edited) using either the mysql console or phpmyadmin check that the queries work using hard coded values, example UPDATE `conversations` SET `sdelete` = '1' WHERE (`conversationID` = '1' OR parent='1') UPDATE `conversations` SET `rdelete` = '1' WHERE (`conversationID` = '1' OR parent='1') If they work, then make sure the PHP code actually works by actually submitting data to it via form <form action="delpage.php" method="post"> ID: <input type="text" name="id" value="" placeholder="Enter id value" /> <input type="submit" /> </form> Edited October 24, 2013 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455204 Share on other sites More sharing options...
mdmartiny Posted October 24, 2013 Author Share Posted October 24, 2013 I did that and it works when I do it that way or when I manually put the number in. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455205 Share on other sites More sharing options...
Ch0cu3r Posted October 24, 2013 Share Posted October 24, 2013 What is the variable $session_user_id set to? It will only update the table if the sender or receiver matches the value of that variable. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455207 Share on other sites More sharing options...
mdmartiny Posted October 24, 2013 Author Share Posted October 24, 2013 That is whoever is logged in at the time. In my case to 1. Quote Link to comment https://forums.phpfreaks.com/topic/283205-ajax-not-sending-info-to-page/#findComment-1455212 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.