Search code examples
phpjavascripthtmlrating

Star rating with two GET


I´ve got a star image as <img>. This image has a javascript function, that is called on click. The javascript method is called addRating(var data). data is the rating number(1-10).

Now in the javascript function, I want to call the php rate_recipe that wants two GET, one for the rating and one for the recipe_id.

How can I "pass" two things to the php? Can I just add two $_GET ?

EDIT1: this is the code of the php:

            include 'db_connect.php';
    mysql_query("SET NAMES 'utf8'");
    mysql_query("SET CHARACTER SET 'utf8'");
    $rate = mysql_real_escape_string($_GET['rate']);
    $exist = 0;
    $recipes_id = mysql_real_escape_string($_GET['recipes_id']);

.. so now, how can I call it in addRating(var data)? I want to stay at the same page, just want to do the rate.


Solution

  • If you want to call a PHP function via JavaScript, you'll need to do so via AJAX. If you're using plain JavaScript, you'll use the XMLHttpRequest object:

    Or if you're using jQuery (recommended):

    Now since you'll be collecting data, the proper convention is to post the data. Here's an example of how to do it with jQuery:

    $.ajax({
        type: "POST",
        url: "some.php",
        data: "rating=10&id=5997"
    }).done(function( msg ) {
        alert( "Data Saved: " + msg );
    });
    

    Where some.php is the location of your PHP script.