0

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 Answer 1

1

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!");
  }
});
Sign up to request clarification or add additional context in comments.

1 Comment

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.

Your Answer

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.