views:

19

answers:

1

I've run into a difficult issue with ajax. I've got 2 files. 1 file calls the other, using the .ajax api. So far I have no issues.

The problem I'm running into is due to how I return a response to the ajax, from the 2nd file. Usually I use the php "return" function. Which then lets me decide what to do with that text. However, I really really want to use the die() function on the 2nd file.

File 1:

$.ajax({
    type:"POST",
    url:"/myfile.php",
    data:"formId=processorder",
    error:function(){alert("error occurred");},
    success:function(response){alert(response);}
});

File 2: (myfile.php)

<?php

  // this does not work
  die("Message I want to return");


  // this does work, but don't want to use it
  return "Message I want to return";

?>

I want to use die() because it stops the process completely. Am I doing something wrong?

I suppose doing both would possibly be the work-around. But that seems a little overkill. So, I thought I'd ask first.

+1  A: 

anything you 'echo' will be sent back in the response. So you could...

echo "Message I want to return";
die();
Jacob
That's what I thought.I've used die() plenty of times before. And it always seemed to perform the same as 'echo', at least in this aspect. But I'm obviously not correct. :)
it is strange that die doesn't return the value as according to the php manual, die(status) is equivalent to exit(status) which "If status is a string, this function prints the status just before exiting." Maybe try exit instead of die and see if it returns a response?
Jacob