0

Hi
I have a code that should:-
if the number entered more than 60
the following should be printed
"Congrats!!!<p>"
"Grade1"
if the number entered between 60 and 40 the following should be printed
"Grade2"
else the script should print
"fail"
but the code print fail all times
can someone help me please.

<html>
<body>
<?php
$A=$_GET['n1text'];
if($A>60)
 {
 echo "Congrats!!!<p>";
echo "Grade1";
}
 else 
{
if($A <60 and $A >= 40)
{
 echo "Grade2";
 } 
 else  
{
echo "\n Fail";
}
} 
 
?>
</body>
</html>
4
Contributors
20
Replies
21
Views
6 Years
Discussion Span
Last Post by ko ko
Featured Replies
  • try[code=php]<?php $A=floatval($_GET['n1text']); // if it is not possible for $a to have a decimal component or if the decimal part is not significant intval() can be used if($A>=60) { echo "Congrats!!!<br>Grade1"; } elseif($A <60 and $A >= 40) { echo "Grade2"; } else { echo "Fail"; } ?>[/code]60 was excluded … Read More

  • 2

    [QUOTE]<input type="text" value="n1text">[/QUOTE] Replace above with below one: [code] <input type="text" name="n1text"> [/code] Read More

  • 1

    [CODE]<html> <body> <?php $A = floatval($_GET['n1text']); if($A>=60) { echo "Congrats!!!<p>Grade1"; } elseif ($A <60 && $A >= 40) { echo "Grade2"; } else { echo "\n Fail"; } ?> </body> </html>[/CODE] Try with above. It works on my PC. Read More

1

try

<?php $A=floatval($_GET['n1text']); // if it is not possible for $a to have a decimal component or if the decimal part is not significant intval() can be used
if($A>=60) { echo "Congrats!!!<br>Grade1"; }
elseif($A <60 and $A >= 40) { echo "Grade2";  }
else { echo "Fail"; } ?>

60 was excluded in the op, > 60 or < 60, no =60

$a is likely being treated as text, by convention all the request arrays are parsed as text, so the original value may be interpreted as "60"

Edited by almostbob: n/a

0
<html>
<body>
<?php
$A=floatval($_GET['n1text']);
if($A>=60)
 {
 echo "Congrats!!!<p>Grade1";
 }
elseif ($A <60 and $A >= 40)
 {
 echo "Grade2";
 } 
else  
{
echo "\n Fail";
} 
?>
</body>
</html>

Same Result!.

0

show the code used to call the file,
is ?n1text=60 in the link
is $_get the right array, $_post $_request ?

0

show the code used to call the file

<html>
<body>
<b>Grades</b>
<form method=get action="Grade.php">
enter your percentage:
<input type="text" value="n1text">
<br>
<input type="submit" name="GRADE" Value="GRADE">
<br>
</body>
</html>
0

Hi vanpersie,

Are you sure the correct value for $_GET is being passed through? After you assign $A try echoing it to see if it is what you expect.


Zagga

0

Hi vanpersie, After you assign $A try echoing it to see if it is what you expect.


Zagga

each time the output is 0 Fail

<html>
<body>
<?php
$A;
$A=floatval($_GET['n1text']);
if($A>=60)
 {
echo $A;
 echo "Congrats!!!<p>Grade1";
 }
elseif ($A <60 and $A >= 40)
 {
echo $A;
 echo "Grade2";
 } 
else  
{
echo $A;
echo "\n Fail";
} 
?>
</body>
</html>
0

Hi again,

I meant after line 5 add a line like this

echo "The value of the variable is: " . $A;

What does it echo?

0

Hi again,


What does it echo?

The value of the variable is: 0 Fail

0

The value of the variable is: 0 Fail

Your code is assuming the variable is a number and checking for numerical values. '0 Fail' is a string not an integer.

You need to ensure $A is a numerical value for your current code to work.

0

floatval() is to convert the text variable to a numeric <form method=get action="Grade.php"> should be <form method='get' action="Grade.php"> dependant on html doctype used, it makes a big difference

2

<input type="text" value="n1text">

Replace above with below one:

<input type="text" name="n1text">
Votes + Comments
Well spotted
good eyes zero
0

Hello Again
I have done the changes that are posted in the two later replies
but with sorry is the same
when I get rid from the floatval function
The output is:-
The value of the variable is: Fail
Hope you help
thank you

0

Echo the value first, and ensure that the parameter takes the correct data. Put this line on the first line of 'Grade.php'.

echo $_GET['n1text'];

If it returns the correct value, then try with floatval() and check again. Hope this help.

0

Echo the value first, and ensure that the parameter takes the correct data. Put this line on the first line of 'Grade.php'.

echo $_GET['n1text'];

If it returns the correct value, then try with 'floatval()' and check again. Hope this help.

0

Any Number is outputted with fail

50 Fail

<html>
<body>
<?php

echo $_GET['n1text'];
 
 
if($A>=60)
 {
 
 echo "Congrats!!!<p>Grade1";
 }
elseif ($A <60 and $A >= 40)
 {
 
 echo "Grade2";
 } 
else  
{
 
echo "\n Fail";
} 
?>
</body>
</html>
1
<html>
<body>
<?php
 $A = floatval($_GET['n1text']);
 
if($A>=60)
 {
 
 echo "Congrats!!!<p>Grade1";
 }
elseif ($A <60 && $A >= 40)
 {
 
 echo "Grade2";
 } 
else  
{
 
echo "\n Fail";
} 
?>
</body>
</html>

Try with above. It works on my PC.

0

Oh
Idon't try the floatval function
After using it the code work perfectly
thank you

<html>
<body>
<?php

 
$A=  floatval($_GET['n1text']);
 
 
if($A>=60)
 {
 
 echo "Congrats!!!<p>Grade1";
 }
elseif ($A <60 and $A >= 40)
 {
 
 echo "Grade2";
 } 
else  
{
 
echo "\n Fail";
} 
?>
</body>
</html>
This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.