0

I have this but is not working which is a mixed of both answers found here (http://stackoverflow.com/questions/10369432/passing-link-with-parameters-with-jquery)... the alert message comes out empty and therefore the delete.php doesn't do anything

Any comments. I am using JQuery 1.8

<!DOCTYPE html>
<html>
<head>
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(function(){
 $(".delete").click(function(e){
 var del_id = $(this).attr("id");
 e.preventDefault();
 $.post("../folder/delete.php", { iid: del_id } ,function(data){
 alert(data)
 //change the link to deleted
 $('#'+del_id).replaceWith('<a href="#" class="delete">Deleted</a>');
 });
 });
});
</script>
</head>
<body>
 <a class="delete" id="1" href='#'>Delete 1</a>
 <a class="delete" id="2" href='#'>Delete 2</a>
 <a class="delete" id="3" href='#'>Delete 3</a>
</body>
</html>

Delete.php is as follows:

<?php 
 extract($_POST); 
 extract($_GET); 
 if ($del_id!=0){ 
 require_once("../includes/include.php"); 
 deleteItem($del_id); 
 } 
?>
Tom Walters
15.6k7 gold badges59 silver badges75 bronze badges
asked Dec 14, 2012 at 19:23
10
  • 2
    show us code for delete.php Commented Dec 14, 2012 at 19:24
  • 1
    Put var_dump($_POST);exit(1); on top of your delete.php file, at least you'll see if your variables are set up correctly. Commented Dec 14, 2012 at 19:30
  • The delete.php is as follows: <?php extract($_POST); extract($_GET); if ($del_id!=0){ require_once("../includes/include.php"); deleteItem($del_id); } ?> Commented Dec 14, 2012 at 19:36
  • 1
    delete.php doesn't print anything, so what do you expect to see in data? Commented Dec 14, 2012 at 19:45
  • 1
    @Jorge Have you read Sheikh Heera's answer? He explains why $del_id is undefined. Commented Dec 14, 2012 at 19:54

1 Answer 1

1

You should change following line

if ($del_id!=0)

with this

if ($iid!=0)

because you are sending

{ iid: del_id } // iid is the variable

from the client side and $iid will be available in the $_POST so you can use without extracting the full $_POST array

if ($_POST['iid']!=0){...}

Also if you don't echo/print anything from the server side then you can't get back anything in the response.

answered Dec 14, 2012 at 19:50

5 Comments

Alternatively, you can change the $.post parameter to { del_id: del_id }.
Thanks for pointing that out. I already tried also with if ($_POST['iid']!=0){...} and doesn't do anything or print anything.
You must use echo/print from the server to send it back in the response, if you use echo "OK" then you'll get OK in the data.
It was my mistake. I was missing something at the delete.php file and now is working! Thanks for taking your time to help me out! Have a great day! Worked with if ($_POST['iid']!=0){...}
One more thing how could I place a confirmation before deleting in the JQuery script?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.