0

i'm new to AJAX and i'm still learning, i use it to delete an user account.

So, thats my main.js code:

function userDeleteAccout() {
 $.ajax({
  type: "post",
  url: "ajax.php?action=delete",
  error:function(){
   alert("Something went wrong!");
  }
 });
}

Runs perfect.

My ajax.php should be like:

if() {
 header("");
 OR
 echo "";
}
else {
 header("");
 OR
 echo "";
}

But this doesn't run. So i need to handle the result, again (using AJAX?) Thank you!

2 Answers2

0
$action = $_REQUEST['action'];
if($action == 'delete') {
 // perform delete operation
}

echo '1';
exit;
Kautil
  • 1,321
  • 9
  • 13
  • 1
    in response you will get 1 so just check eg. if(response == '1') { alert(1); } – Kautil Jun 25 '13 at 09:23
  • @MathisSchülingkamp What did you expected to happen? – fGo Jun 25 '13 at 10:05
  • @MathisSchülingkamp Nothing *will* happen with that echo. What you're doing is running a server side script. That script won't perform any operation on the client side. But if you check the response using `done` it should have the content containing what you echoed out in the PHP – andy Jun 25 '13 at 10:08
0

A couple of issues here.

Firsly you're sending your request as POST in the javascript:

type: "post"

But then passing your data via the URL string:

url: "ajax.php?action=delete"

What you probably need to do is something like this:

Javascript

function userDeleteAccout() {
  $.ajax({
      type: "post",
      data: { action: "delete" },
      url: "ajax.php",
      error:function(){
         alert("Something went wrong!");
      }
  });
}

PHP

$data = $_POST['data'];

if($data['action'] == "delete")
{
  doSomething();
  echo "Delete successfull";
}else{
  doSomethingElse();
  echo "Something else done";
}

You should check out how to manage requests and responses in JSON. This is helpful for a beginner: http://www.lennu.net/2012/06/25/jquery-ajax-example-with-json-response/

Handling data coming back you can use .done():

function userDeleteAccout() {
  $.ajax({
      type: "post",
      data: { action: "delete" },
      url: "ajax.php",
      error:function(){
         alert("Something went wrong!");
      }
  }).done(function(msg)
  {
     alert(msg);
  });
}

Check out the jQuery docs here: http://api.jquery.com/jQuery.ajax/

Security

But just as a last point, please take security into consideration here as anyone can send a JS request to this url forcing a delete of some sort.

I can go to your page, open my dev tools and run a html request with the post data {action: 'delete'} and send the request to your ajax script thus deleting something.

So the main thing to do is:

Community
  • 1
  • 1
outrunthewolf
  • 373
  • 3
  • 13
  • This looks good, but i wonder that nobody seems to get my problem :/ i can't ECHO, nothing happend :o Look: I have a in a bootstrap modal, than it runs the AJAX function, goes to the ajax.php BUT: It doesn't ECHO anything on the page! – Mathis Schülingkamp Jun 25 '13 at 09:28
  • 1
    add something like `htmlEntities($_POST['data'])` in your php code refer here `[link](http://php.net/manual/en/function.htmlentities.php)` – Nesmar Patubo Jun 25 '13 at 09:40
  • Depending on what you're trying to accomplish (there are many ways to do what you're doing), you can update the pages html from the response with `$('#element').html(response)` – outrunthewolf Jun 25 '13 at 09:51
  • You can't echo to the page from your ajax PHP file, see in the above example how the done function is used and the text you echo in your php file gets returned in the msg variable. You can then use an alert or add this into some html on the page using jquery $("#some_div").html(msg) – MattP Jun 25 '13 at 11:37
  • 1
    Don't put quotes around the msg variable or it will just show the string. Use alert(msg) or alert('' + msg + '') i've seen before – MattP Jun 25 '13 at 11:40