1

Hey I know that they are many threads about the theme but i need your help because i am a complete noob in AJAX / JQuery stuff ://

I just want that my site doesn´t reload when somebody votes. I have two scripts (an example for thumbsdown)

if (!isset($_COOKIE["vote" . $_GET['id'] . ""])) {
    header("Location: http://localhost/votedown.php?id=" . $_GET['id'] . "");
} else {
    header("Location: http://localhost/video.php?id=" . $_GET['id'] . "");
}

And here votedown.php

include('config.php');
setCookie("vote" . $_GET[ 'id' ] . "", "true", time() + 3600);
mysql_query("UPDATE videos SET `thumbsdown` = `thumbsdown`+1 WHERE id ='" . $_GET[ 'id' ] . "'");
Popnoodles
  • 28,090
  • 2
  • 45
  • 53
  • So, you have the *server* side down, now you need *client* side. Create an AJAX request that will fire *on click* of your voting buttons. The `url` of said AJAX request should point to the PHP file containing the above. If that PHP is on the same page, leave the `url` blank. – tymeJV Dec 22 '13 at 20:32
  • The code in the question has a serious SQL injection vulnerability. Read more here: http://www.php.net/manual/en/security.database.sql-injection.php – Joel L Dec 22 '13 at 23:29

1 Answers1

1

Following on from Marabuntas answer; here's a jQuery solution (which is easier to read than the pure JavaScript version):

$.ajax({
    type: 'GET',
    url: 'votedown.php',
    data: // data to put in database here,
    success: function() {
        // callback here
    }
});
Enijar
  • 6,387
  • 9
  • 44
  • 73