Modern browsers can post a rating without the older XMLHttpRequest/new-window pattern. Return JSON from the PHP endpoint and update the result area only after the request succeeds.
document.querySelector('#rating-form').addEventListener('change', async (event) => {
if (event.target.name !== 'rating') return;
const body = new FormData(event.currentTarget);
const response = await fetch('rating-submit.php', {
method: 'POST',
body,
headers: {'Accept': 'application/json'}
});
if (!response.ok) throw new Error('Rating could not be saved');
const result = await response.json();
document.querySelector('#rating-result').textContent = result.message;
});
The PHP endpoint should validate the 1–5 value, verify the page/content identifier, check the CSRF token, use a prepared statement, and enforce the application's duplicate-vote or rate-limit policy.
CREATE TABLE IF NOT EXISTS `plus2net_rating` (
`rating_id` int(10) NOT NULL AUTO_INCREMENT,
`rating` int(1) NOT NULL,
`page_name` varchar(100) NOT NULL,
PRIMARY KEY (`rating_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;
require "common-tags2.php";
Inside this common-tag2.php page we will keep the radio buttons to receive user selection. Each radio button is connected to Ajax function by onClick event and it passes the rating value to the ajax function.
<?Php
echo "<br><br><div id='rating-box' style="text-align:center;border:1px solid red;width:550px;">Please rate this Article on one to five scale<br>";
echo "<input type=hidden id=page_name name=page_name value='$page_name'>
<INPUT TYPE=RADIO NAME=rone Value=1 onClick='ajax_rating_Function(1)';><img src=images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=2 onClick='ajax_rating_Function(2)';><img src=images/star.gif><img src=images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=3 onClick='ajax_rating_Function(3)';><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=4 onClick='ajax_rating_Function(4)';><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif> <INPUT TYPE=RADIO NAME=rone Value=5 onClick='ajax_rating_Function(5)';><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif><img src=images/star.gif>";
echo" </div>";
?>
$sql=$dbo->prepare("insert into plus2net_rating (rating,page_name) values(:rating,:page_name)");
$sql->bindParam(':rating',$rating,PDO::PARAM_INT, 1);
$sql->bindParam(':page_name',$page_name,PDO::PARAM_STR, 100);
select count(rating_id) as no, FORMAT(avg(rating),1) as avg1, page_name from plus2net_rating group by page_name
select count(rating_id) as no, FORMAT(avg(rating),1) as avg1, page_name from plus2net_rating group by page_name order by avg1 desc limit 0, 5
require "../common-tags2.php";
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.
| smo123 | 11-04-2013 |
| Can you integrate this script to my website? | |