Jump to content


Photo

if help


  • Please log in to reply
1 reply to this topic

#1 Tandem

Tandem
  • Members
  • PipPipPip
  • Advanced Member
  • 251 posts

Posted 26 September 2006 - 02:45 PM

I'm having some trouble with something....

Here is some code i wrote just to give an example:
<html>
<head>
</head>
<body>
<?



$output = $_POST['output'];

if ($output == "nothing") {
$output = 0;
}

if ($output == "something") {
echo"<br>Hello";
}


echo <<<HERE
<form method="POST" action="forms.php" name="f">
<input type="text" name="output" value="">
<input type="submit" name="submit" value="Submit!">
</form>
HERE;
?>
</body>
</html>

In this form if you enter the word 'something' into the text input it will echo "Hello". If you enter the word 'nothing' it will set the the variable $output to 0, but then for some reason "Hello" is echoed.

Why is this, and what ways are there to stop this?

#2 alpine

alpine
  • Members
  • PipPipPip
  • Advanced Member
  • 756 posts
  • LocationNorway

Posted 26 September 2006 - 03:27 PM

Well, your actually setting the comparasor test to if(0 == "something") - always returning true

This works, setting to false instead

if ($output == "nothing") {
$output = false;
}

if ($output == "something") {
echo"< br >Hello";
}


But if your purpose with this is to check if formfields are submitted empty or not, use

if(!empty($field))
{
// not empty
}
else
{
// empty
}





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users