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>

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"

<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!.

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

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>
Member Avatar for Zagga

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

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>
Member Avatar for Zagga

Hi again,

I meant after line 5 add a line like this

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

What does it echo?

Hi again,


What does it echo?

The value of the variable is: 0 Fail

Member Avatar for Zagga

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.

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

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

Replace above with below one:

<input type="text" name="n1text">
commented: good eyes zero +13
commented: Well spotted +5

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

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.

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.

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>
<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.

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>

Mark this thread as Solve then, and it will help us to save our time.

ok

Click 'Mark this Thread as Solve' link below of these answers.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.