I need fa fa-star class's mouseenter, mouseout, and onclick function work for each specific 'list' class/li tag(like list class[0, 1, 2, etc]). What I have now is working for every 'list' class. When I mouseenter on a star from different list class the 1st list class's star is changing.
If I mouseenter on a star from 1st list class/li tag the mouseenter function should work for that list class/li tag not for other 'list' class/li tag. if 2nd 'list' class then it should work for that 'list' class/li tag not for others. and for 3rd, 4th etc.
in rateclick function I can send the rating to server but can't fetch it. Can you look into it also?
Here is the code:
index.php and style.css
.fa{
font-size:25px;
}
li{
list-style:none;
padding-left:40px;
padding-bottom:40px;
border-bottom:2px solid #ccc;
width:800px;
}
li:last-child{
border-bottom:none;
}
<?php
require "conx.php";
$sql = "SELECT * FROM customers ORDER BY id ASC";
$query = $conx -> query($sql);
$numrows = $query -> num_rows;
?>
<!DOCTYPE html>
<html>
<link rel="stylesheet" href="css/font-awesome.min.css">
<link rel="stylesheet" href="style.css">
<script src="jquery.min.js"></script>
<body>
<div class="Insert-Into">
<ul>
<?php if($numrows > 0): ?>
<?php while($row = $query -> fetch_assoc()): ?>
<?php $rating = $row["rating"]; ?>
<?php $customerid = $row["id"]; ?>
<li data-rating=<?php echo "$rating"; ?> data-customerid=<?php echo $customerid; ?> class="list">
<br/>
<br/>
<span class="username"><?php echo $row["username"]; ?></span>
<br/>
<br/>
<?php
$i;
$color;
for($i = 1; $i <= 5; $i++){
if($i <= $rating){
$color = "color:orange";
}
else{
$color = "color:#ccc";
}
echo "<i class='fa fa-star' style='$color;' data-rating='$i' onmouseenter='mouseenterrating($i)' onmouseout='clearstar($rating)' onclick='rateclick($customerid, $i)'></i>";
}
?>
<br/>
<br/>
<b>Description</b>
<br/>
<br/>
<div class="desc"><?php echo $row["description"]; ?></div>
</li>
<?php endwhile; ?>
<?php endif; ?>
</ul>
</div>
<script>
function mouseenterrating(count){
var j;
var fa = document.getElementsByClassName("fa");
for(j = 0; j < fa.length; j++){
if(j < count){
fa[j].style.color = "orange";
}
else{
fa[j].style.color = "#ccc";
}
}
}
function clearstar(rating){
var fa = document.getElementsByClassName("fa");
var k;
for(k = 0; k < fa.length; k++){
if(k < rating){
fa[k].style.color = "orange";
}
else{
fa[k].style.color = "#ccc";
}
}
}
function rateclick(customerid, count){
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "rating.php?id="+customerid+"&rate="+count);
xhttp.send();
fetch(customerid);
}
function fetch(e){
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "fetch.php");
xhttp.send();
}
</script>
</body>
</html>
rating.php
<?php
require "conx.php";
if(isset($_GET['id']) && isset($_GET['rate'])){
$customerid = $_GET['id'];
$rate = $_GET['rate'];
$sql = "UPDATE customers SET rating = '$rate' WHERE id = '$customerid'";
$result = $conx -> query($sql);
}
?>
fetch.php
<?php
require "conx.php";
$sql = "SELECT * FROM customers WHERE id = '".customerid."'";
$result = $conx -> query($sql);
$fetch = $result -> fetch_assoc();
?>
conx.php
<?php
$conx = mysqli_connect("localhost", "root", "");
if(!$conx){
echo "Not connected with localhost";
}
$sql = "CREATE DATABASE rating";
$query = $conx -> query($sql);
$dbselect = $conx -> select_db("rating");
if(!$dbselect){
echo "No database selected";
}
$sql = "CREATE TABLE customers(id INT(255) NOT NULL AUTO_INCREMENT UNIQUE KEY, username VARCHAR(200) NOT NULL, description VARCHAR(1000) NOT NULL, email VARCHAR(200) NOT NULL PRIMARY KEY, rating INT(5) NOT NULL)";
$query = $conx -> query($sql);
?>