Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
129
rated 0 times [  135] [ 6]  / answers: 1 / hits: 26058  / 13 Years ago, sun, august 14, 2011, 12:00:00

:)



I'm hoping to make a very simple rating system. It won't consist of anything like averages, it's literally vote up or vote down, so if there's more votes down it'll go into a minus stance.



What I'd like is for when the links to vote up/down are clicked, the page isn't refreshed, just that rating number. I'm guessing I can do this with JavaScript's append once it calls the new data, however I've no idea how to run the MySQL query with JavaScript.



From what I understand, this isn't all that safe so I'm hoping I can run it from a PHP file?



Can anyone tell me how to do this please?


More From » php

 Answers
16

You have to have the SQL update query in a PHP file and execute that PHP script via AJAX. For example:



In PHP:



$page_id = mysql_real_escape_string(html_entities($_POST['page_id']));
$rating = mysql_real_escape_string(html_entities($_POST['rating']));

mysql_query( UPDATE ratings(vote) VALUES ('$rating') WHERE id = '$page_id' );


AJAX (assuming you are using jQuery):



function rate(rating, page_id)
{

$.ajax({
url: 'path/to/php_script.php',
type: 'post',
data: 'rating='+rating+'&page_id='+page_id,
success: function(output)
{
alert('success, server says '+output);
}, error: function()
{
alert('something went wrong, rating failed');
}
});

}


HTML:



<form>   
Like: <input type=button value=Like onClick=rate(1, $_GET['page_id']) />
<br />
Hate: <input type=button value=Hate onClick=rate(2, $_GET['page_id']) />
</form>

[#90617] Friday, August 12, 2011, 13 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
kristinsonjab

Total Points: 364
Total Questions: 98
Total Answers: 98

Location: Christmas Island
Member since Mon, Oct 19, 2020
4 Years ago
kristinsonjab questions
Fri, Mar 4, 22, 00:00, 2 Years ago
Fri, Jan 22, 21, 00:00, 3 Years ago
Fri, Aug 14, 20, 00:00, 4 Years ago
;