0

a user need to put a score through prompt box in javascript and then a grade need to echo in php using if-else statement.I wrote the below code but it's not working.Please help me to make it right.Thanks.

<!octype html>
<html>
<body>
<script type="text/javascript">
var t= prompt("Please enter a value between 0 to 100");
<?php $t = "<script>document.write(t)</script>"?>   
</script>
<?php
if ($t >= 80 && $t<=100) {
    echo "Your grade is A+";
} elseif($t >= 70 && $t<80) {
    echo "Your grade is A";
} elseif($t >= 60 && $t<70) {
    echo "Your grade is B";
}elseif($t >= 50 && $t<60) {
    echo "Your grade is C";
}else {
  echo "Your grade is Fail";
}
?>
</body>
</html>
Newage
  • 1
  • 1
  • What exactly is not working? – Jan May 19 '16 at 07:03
  • The if-else statement is not working.whatever value is put in prompt box it is displaying "Your grade is Fail" which was the last statement in above code. – Newage May 19 '16 at 07:55

1 Answers1

-1

Updated code using php

<html>
<body>
<script type="text/javascript">
var t= prompt("Please enter a value between 0 to 100");
document.cookie="score=" + t;   
</script>
<?php
$t = $_COOKIE['score'];
if ($t >= 80 && $t<=100) {
    echo "Your grade is A+";
} elseif($t >= 70 && $t<80) {
    echo "Your grade is A";
} elseif($t >= 60 && $t<70) {
    echo "Your grade is B";
}elseif($t >= 50 && $t<60) {
    echo "Your grade is C";
}else {
  echo "Your grade is Fail";
}
?>
</body>
</html>
Jignesh Patel
  • 1,028
  • 6
  • 10
  • It works.thanks. but since I need to solve this using php so is there any other way to solve this using if-else statement and 'echo' in php? – Newage May 19 '16 at 07:57
  • Please check updated code – Jignesh Patel May 19 '16 at 08:10
  • 1
    That won't work. The PHP will run, output "Your grade is Fail" (and a bunch of warnings about undefined variables), send the output to the browser and then (when it is far, far too late) prompt for a value. – Quentin May 19 '16 at 08:14
  • @Jignesh Patel: with the updated code,I'm facing a problem.When I enter a number in prompt box-first time it's displaying wrong statement and if I enter the same value 2nd time then it's displaying right statement.How can I solve this please?thanks. – Newage May 19 '16 at 08:40
  • You have to use ajax request to pass variable from javascript to php – Jignesh Patel May 19 '16 at 08:57