Posted By

dropthenerd on 02/02/11


Tagged

json


Versions (?)

Example of a JSON encoded repsonse


 / Published in: PHP
 

URL: http://www.dropthenerd.com/ajax-a-simple-approach-that-you-will-use-and-reuse/

  1. <?php
  2.  
  3. $answer = $_POST['answer'];
  4.  
  5. if($answer == '8'){
  6.  
  7. echo json_encode(
  8. 'success' => 1,
  9. 'message' => 'Correct! 3 + 5 = 8'
  10. )
  11. );
  12.  
  13. exit();
  14.  
  15. }else{
  16.  
  17. echo json_encode(
  18. 'success' => 0,
  19. 'message' => 'Sorry, that is the wrong answer.'
  20. )
  21. );
  22.  
  23. exit();
  24.  
  25. }
  26.  
  27.  
  28. // just to have an extra failsafe!
  29. echo json_encode(
  30. 'message' => 'You must choose a valid response.'
  31. )
  32. );

Report this snippet  

You need to login to post a comment.