Jump to content

Send a boolean with a form


umekille78
 Share

Recommended Posts

Hi all!I have a problem in my page. One page tries to send a boolean with a form like this:

<input type="hidden" name="flag" value=FALSE>

When I send this to my other page that page handes $flag like a string.I tried a IF statment on it and it returns as if TRUE. I used print to look at her $flag and it was FALSE as it should. The only way I got it to work right was to do a IF statment like this:

if($flag == "TRUE")

I dont know how to get this to work right. Help needed./Per

Link to comment
Share on other sites

Made some testing:

<?phpif (!isset($_GET['flag1'])) {?><html><head><title>title</title></head><body><form action="?" method="get"><input type="hidden" name="flag1" value="FALSE"><input type="hidden" name="flag2" value="0"><br><input type="submit" value=" SUBMIT "></form></body></html><?php} else {	$flag1 = $_GET['flag1'];	$flag2 = $_GET['flag2'];		echo "flag1: ".$flag1."<br>";	echo "flag2: ".$flag2."<br><br>";		echo "flag1 is a ".gettype($flag1)."<br>";	echo "flag2 is a ".gettype($flag2)."<br>";		echo "<br>flag1 is false? ";	if ($flag1 == FALSE)  echo "yes";  else  echo "no";		echo "<br>flag2 is false? ";	if ($flag2 == FALSE)  echo "yes";  else  echo "no";  }?>

It seems that:-With or without the quotes, the HTML form still sends a string, not a boolean.-PHP sometimes makes the casting automaticaly (like between strings and integers), but it doesn't cast the string "FALSE" to the boolean FALSE-PHP is casting automaticaly the string "0" to the boolean FALSESo it will work with <input type="hidden" name="flag" value="0">But I would use $flag = (bool) $flag; 'cause I just like it that way... :) Makes you look smarter :)

Link to comment
Share on other sites

So I was right after all, it did not work due to the value of the input..Between brackets, did you know this could be smaller?

if ($flag1 == FALSE) echo "yes"; else echo "no";
shorten it to this:
echo ($flag1 == false) ?"yes" :"no";
exactly the same :)
Link to comment
Share on other sites

:) yes, I knew about short 'if'.And about the value of the input, it does work for the "0" value, but not for the "FALSE" value.This is the output of my script:flag1: FALSEflag2: 0flag1 is a stringflag2 is a stringflag1 is false? noflag2 is false? yesSo "FALSE" is not recognized being FALSE, only "0" is.Ok, it's enough for today... I get back to the work I am paid for :)
Link to comment
Share on other sites

aspnetguy is correct, all form variables are always strings. Both the request from the client to the server and the response from the server to the client are each just long strings of text, there are no boolean or integer values, just strings that get interpreted different ways.All of these values will evaluate to FALSE if you use loose comparison ($x == false):boolean FALSEempty string ("")undefined variable0"0"nullarray()Everything else is true. All non-empty strings are considered to evaluate to true, and so the string "false", being a non-empty string, evaluates to true. The only exception is the string "0", this was changed in PHP4 to evaluate to false.But you can always do strict comparison. This expression will evaluate to true:("true" == true)but not this one:("true" === true)http://us2.php.net/manual/en/types.comparisons.phpWhen I'm dealing with form input, I ignore casting and always check for a string.if ($_POST['checkbox'] == "true")

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...