I need to temporarily store the value in a PHP variable because the value should be sent together with a comment. A user cannot send a rating without entering a comment, that’s why I decided to make this.
Why is that when PHP calculates the value my variable returns a value of zero?
I have this code:
<a href='#' onclick='rate(1); return false;' class='one-star'>1</a>
<script type="text/javascript">
function rate (amnt){
<?php
$currentRating = amnt;
$ratingImageWidth = $currentRating * 25;
echo "alert($ratingImageWidth)";
?>
}
</script>
Note:
$currentRating variable is not null, I tried to include it in the alert function, and it returns the right value.
$currentRating variable to an int:
settype($currentRating, "integer");
(int)$currentRating;
Nothing happened, it still returns Zero. does anyone know how to solve this? Big thanks to those who will help.
PHP runs on the server, so your JavaScript actually looks like:
It’s surprising that it works at all given the missing
}at the end…The point is, you can’t mix PHP and JS in this way. Why don’t you just do
alert(amnt*25)?