Click here to Skip to main content
15,902,777 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I'm trying to check a number is even or odd on a php page. I write my code but i have a problem. When i run it, it gets 0 as default number and also after checking the number box become empty. Could you help me?

HTML
<html><head></head>
<body>
<form action="PAGEName.php" method="post">
	<table bgcolor="yellow" border="5" bordercolor="black"> <tr> <td>
	Is   <input type="text" size="6" value="" name="input">  
	<input type="submit" value="even or odd?"> <br>
	Answer: <input type="text" size="8" 
	value=" 
	    $n=$_POST['input'];
	print($n%2==1 ? 'odd' : 'even');
	?>
    " 
	name="result"> <br>	
	</br></br></td> </tr> </table>

</body></form></html>
Posted
Updated 21-Oct-12 11:31am
v2

1 solution

I think you cann't add PHP in the value attribute.
So, you need to add 'even' or 'odd' like this:

PHP
<?
if ((int)$_POST['input'] % 2 == 1)
{
echo "odd";
}
else
{
echo "even";
}
?>
 
Share this answer
 
v2
Comments
sesenyg 21-Oct-12 13:07pm    
It still behaves the same, it gets 0 default.
sesenyg 21-Oct-12 13:10pm    
or in other words, it gets even in default.
Thomas Daniels 21-Oct-12 13:11pm    
Then I think there's something wrong in the POST-form.
sesenyg 21-Oct-12 13:15pm    
OK, now i did it truely. I add one more if clause like
<input type="text" size="8"
value="
sesenyg 21-Oct-12 13:17pm    
Thanks for your attention:)

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900