Jump to content

Ajax Not sending info to page


mdmartiny

Recommended Posts

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

Link to comment
Share on other sites

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 by gristoi
Link to comment
Share on other sites

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);
		} 
?>
Link to comment
Share on other sites

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 by Ch0cu3r
Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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 by Ch0cu3r
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.