tags:

views:

40

answers:

1

I have a form that is submitted via Ajax to a PHP script. Inside the PHP script I do a few checks for swear words.

How would I pass a 'yes' or a 'no' back to jquery from the PHP script so I can display some feedback to the user on the front end? In other words, a 'Thank you', or a 'Don't Swear!'

Thanks

+1  A: 

You can just echo "yes" or "no", and do some string comparison in the jQuery success function. You could also echo "true" or "false", eval the content of the response and use it as a boolean, or use JSON...

With JSON, the php will look like :

<?php

...

echo "{\"result\" : true}";

?>

The javascript :

$.getJSON('blabla.php', function(data) {
  if(data.result){
    alert("dont't swear!");
  }else{
    alert("good boy!");
  }
});
Pierre Henry
Probocop: the 'data' that Pierre shows passed to the function in the $.get above represents what is returned or echoed from your 'blabla.php' script. 'data.result' represents the JSON object returned. Sort of like using an array variable and index in PHP. If you echo a string in 'blabla.php' instead of return JSON, 'data' will represent the string. If you return a variable or array, 'data' represents that as well. Very flexible.
kevtrout