I'm trying to implement a simple rating system in PHP using JavaScript, jQuery and MySQL. The code below returns a success message that the rating was successful (the query was successful) but it does not populate the table.
The "rating" table has 4 fields. int field for ratingID (auto incremented), char field for userID, char field for adventureID and an int field for ratingScore. I've been debugging this for about 35 minutes now and for the life of me can't find the issue with the code.
Submission form:
<div class="stars">
<form role="form" method="post">
<input class="star star-5" id="star-5" type="radio" name="star" val="5" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
<label class="star star-5" for="star-5"></label>
<input class="star star-4" id="star-4" type="radio" name="star" val="4" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
<label class="star star-4" for="star-4"></label>
<input class="star star-3" id="star-3" type="radio" name="star" val="3" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
<label class="star star-3" for="star-3"></label>
<input class="star star-2" id="star-2" type="radio" name="star" val="2" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
<label class="star star-2" for="star-2"></label>
<input class="star star-1" id="star-1" type="radio" name="star" val="1" onclick="rate('<?php echo $getUserID ?>', '<?php echo $adv['adventureID'] ?>')"/>
<label class="star star-1" for="star-1"></label>
</form>
</div>
JavaScript file:
var rating = {
rating: 0,
userID: "",
adventureID: "",
};
function rate(userID, adventureID) {
if(rating.rating = document.getElementById('star-5').checked)
rating.rating = document.getElementById('star-5').value;
else if(rating.rating = document.getElementById('star-4').checked)
rating.rating = document.getElementById('star-4').value;
else if(rating.rating = document.getElementById('star-3').checked)
rating.rating = document.getElementById('star-3').value;
else if(rating.rating = document.getElementById('star-2').checked)
rating.rating = document.getElementById('star-2').value;
else if(rating.rating = document.getElementById('star-1').checked)
rating.rating = document.getElementById('star-1').value;
rating.userID = userID;
rating.adventureID = adventureID;
callPHPrating();
}
function callPHPrating() {
// call ajax
$.ajax({
type: "POST",
url: 'rating_submit.php',
data:{rating:rating.rating, userID:rating.userID, adventureID:rating.adventureID},
success:function(msg) {
alert(msg);
if(msg == "Rating submitted.")
window.location.reload();
//Reload page if successful
}
});
}
rating_submit.php file:
<?php
$ratingInput = $_POST['rating'];
$userid = $_POST['userID'];
$adventureid = $_POST['adventureID'];
$cfg = include('utilities.php');
$con = mysqli_connect($cfg['host'], $cfg['user'], $cfg['password'], $cfg['database']);
$query = "INSERT INTO rating (userID, adventureID, ratingScore) VALUES('$userid', '$adventureid', '$ratingInput')";
mysqli_query($con, $query);
if(!$query)
die('Invalid enquiry '.mysqli_error());
else echo "Rating submitted.";
?>