Jump to content

Ajax is a killer. Can anyone help me delete a php record using Ajax?


man5

Recommended Posts

Right now I redirect to index page after I delete a record.  However I am looking to make it so that I can delete a record without redirecting the page. I know this can be accomplised using Ajax.  I have spent countless hours before trying to make it work, but it did not work.

 

So here is a basic setup I created.   Can you please update it with ajax code so that I can see how it's done properly?

<!DOCTYPE HTML>
<html lang="en"> 
	<head>
		<meta charset="UTF-8">
		<title>Home Page</title>
	</head>
	<body>
	
	<div class="record" >
		<a href="record.php?id=<?php echo $record_id ?>"><?php echo $record_name; ?></a>
		<div class="delete-record">
			<a href="delete.php">Delete Record</a>
		</div>
	</div>	
	
	</body>
</html>
	

Edited by man5
Link to comment
Share on other sites

We are not here to write code for people. Post what you have tried, and a description of your problem.

 

Sure. Here is the updated code.

<!DOCTYPE HTML>
<html lang="en"> 
	<head>
		<meta charset="UTF-8">
		<title>Home Page</title>
		
		<script src="jquery.js"></script>
		   <script type="text/javascript">
			$(function() {
			$(".delete").click(function(){
			var element = $(this);
			var del_id = element.attr("id");
			var info = 'id=' + del_id;
			if(confirm("Are you sure you want to delete this?"))
			{
			 $.ajax({
			   type: "POST",
			   url: "delete.php?id=<?php echo $id?>&user=<?php echo $user; ?>",
			   data: info,
			   success: function(){
			 }
			});
			  $(this).parents(".show").animate({ backgroundColor: "#003" }, "slow")
			  .animate({ opacity: "hide" }, "slow");
			 }
			return false;
			});
			});
		</script>
	</head>
	<body>
	
	<div class="record" >
		<a href="record.php?id=<?php echo $record_id ?>"><?php echo $record_name; ?></a>
		<div class="delete-record">
			<a href="#" id="<?php echo $id; ?>" class="delete" >Delete Record</a>
		</div>
	</div>	
	
	</body>
</html>
Edited by man5
Link to comment
Share on other sites

Well the problem is that I can't get the record to delete using ajax. Above is the setup I have. I need someone to look at it and tell what's wrong with it.

 

Here is the delete.php code.  This query works.

$user_id	= intval(Input::get('user'));
$record_id	= intval(Input::get('id'));
				
$sql = 'DELETE FROM records WHERE id = ? AND user = ?';

$stmt = DB::getInstance()->query($sql, array($record_id, $user_id));
Edited by man5
Link to comment
Share on other sites

I didn't do the onclick  warning, but I figure you can figure that out. That's just making the page spiffy anyways. ;D  This is how I would do it, though it could be modified to fit your needs, specially if you're pulling in the $user variable from someplace else.

<?php 
$id=64; // Temporay id variable:
$user = 'Clint Eastwood'; // Temporary user name variable:
?>
<!DOCTYPE HTML>
<html lang="en"> 
<head>
	<meta charset="UTF-8">
	<title>Home Page</title>
</head>
<body>
	
	<div class="record" >
		<div class="delete-record">
			<a class="delete" data-user="<?php echo $user; ?>" data-id="<?php echo $id; ?>" href="delete.php">Delete Record</a> 
		</div>
    <span id="result"></span>
	</div>	
	<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
	<script>
		$(function() {
			var $delBtn = $('a.delete'),
			    $myURL  = $('a.delete').attr('href'),
			    $user   = $('a.delete').data('user'),
			    $id     = $('a.delete').data('id');
			
			console.log($myURL, $user, $id);
			$delBtn.on('click', function(event) {
				event.preventDefault();
				var params = { id: $id, user: $user }; // Set parameters
				var myData = jQuery.param( params ); // Set parameters to corret AJAX format				
				$.ajax({
					type:"post",
					url:$myURL, // Set the URL to from the href attribute in the anchor tag:
					data: myData,
					success:function(info){
						$('#result').html(info); // Display the result back when deleted: 
					}
				});	// End of Ajax function:							
			}); // End of Delete Button

		});	// End of Doc Ready Function:
	</script>
</body>
</html>

Below is the delete.php file that I tested it out with:

<?php
echo $result = $_POST['id'] . ' ' . $_POST['user'];
Edited by Strider64
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.