Guest User

Untitled

a guest
Dec 10th, 2018
84
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.02 KB | None | 0 0
  1. <!-- HTML -->
  2. <ul>
  3. <li data-id="1">This is a question</li>
  4. <li data-id="2">Another question</li>
  5. </ul>
  6. <!-- /HTML -->
  7.  
  8. <!-- Javascript -->
  9. setTimeout(function(){
  10. $.ajax({
  11. url: 'poll.php',
  12. type: 'GET',
  13. dataType: 'json',
  14. success: function(response){
  15. // Response will be a JSON object returned from the PHP script
  16. check_changed_questions(response);
  17. }
  18. });
  19. });
  20.  
  21. function check_changed_questions(questions){
  22. for(i=0; i<questions.length; i++){
  23. var id = questions[i].id;
  24. var html_question = $('li[data-id=' + id + ']').html();
  25. var db_question = questions[i].questions;
  26.  
  27. if(db_question != html_question){
  28. // Means the question in the database does not
  29. // match the one on the page
  30.  
  31. }
  32. }
  33. }
  34.  
  35. <!-- /Javascript -->
  36.  
  37. <!-- PHP (poll.php) -->
  38. <?php
  39. $sql = "SELECT * FROM questions";
  40. $data = array();
  41. $result = mysql_query($sql);
  42.  
  43. while($row = mysql_fetch_assoc($result))
  44. {
  45. $data[] = array('id' => $row['id'], 'question' => $row['question']);
  46. }
  47.  
  48. echo json_encode($data);
  49. ?>
Add Comment
Please, Sign In to add comment