Guest User

Untitled

a guest
Aug 13th, 2018
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.31 KB | None | 0 0
  1. Making a Like/Dislike AJAX script - Not working
  2. <div id="voting">
  3. <form>
  4. <input name="vote" type='button' onclick="getVote(<?php echo $image['filename'];?>)" value='Like' />
  5. </form>
  6. </div>
  7.  
  8. <script type="text/javascript">
  9. function getVote(filename)
  10. {
  11. if (window.XMLHttpRequest)
  12. {// code for IE7+, Firefox, Chrome, Opera, Safari
  13. xmlhttp=new XMLHttpRequest();
  14. }
  15. else
  16. {// code for IE6, IE5
  17. xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  18. }
  19. xmlhttp.onreadystatechange=function()
  20. {
  21. if (xmlhttp.readyState==4 && xmlhttp.status==200)
  22. {
  23. document.getElementById("voting").innerHTML=xmlhttp.responseText;
  24. }
  25. }
  26. xmlhttp.open("GET","voting.php"+filename,true);
  27. xmlhttp.send();
  28. }
  29. </script>
  30.  
  31. <?php
  32. //Database Information
  33. $dbhost = "";
  34. $dbname = "";
  35. $dbuser = "";
  36. $dbpass = "";
  37.  
  38. //Connect to database
  39. mysql_connect ( $dbhost, $dbuser, $dbpass)or die("Could not connect: ".mysql_error());
  40. mysql_select_db($dbname) or die(mysql_error());
  41.  
  42. $filename = $_GET['filename'];
  43.  
  44. $query = "UPDATE images SET rating = rating+1 WHERE filename = '$filename'";
  45.  
  46. mysql_query($query) or die(mysql_error());
  47. mysql_close();
  48.  
  49. ?>
  50.  
  51. xmlhttp.open("GET","voting.php?filename="+filename,true);
  52.  
  53. $filename = isset($_GET['filename']) ? mysql_real_escape_string($_GET['filename']) : null ;
  54.  
  55. if (is_null($filename)) {
  56. exit;
  57. }
Add Comment
Please, Sign In to add comment