forms - php loads before hitting ‘submit’ even when using if(isset($_GET["submit"])); ignores constant ‘GREETING’ -
i have been working on simple ‘get’ form on week , calling uncle. want form echo greeting constant before user hits ‘submit’- i.e. when page first loads. without hitting ‘submit’ response ‘no user input’ loads , ignores greeting. here code w/out if(isset($_get[“submit”])).
when add if(isset..) greeting constant loads other actions ignored.
code w/out isset:
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>a simple form</title> </head> <body> <form name="getform" action="<?php echo htmlentities($_server['php_self']);?>" method="get"> 1) colors of u.s. flag?<br> <input type="radio" name="question1" value="a" />a) red, white , blue<br /> <input type="radio" name="question1" value="b" />b) yellow, red , blue<br /> <input type="radio" name="question1" value="c" />c) blue, green , amber <br> <input type="submit" value="go" /><input type="reset" value="reset" /> </form> </body> </html> <? define(error, 'no answer input question. please select , answer.'); define(greeting, 'welcome simplest of php forms.'); $answer1=$_get['question1']; if($answer1=="a") {echo "you correct."; } elseif ($answer1=="") {echo error. "" ; } elseif ($answer1=="b" || $answer1=="c") {echo "your answer incorrect."; } else {echo greeting. ""; } ?>
here code if(isset..
<!doctype html public "-//w3c//dtd xhtml 1.0 transitional//en" "http://www.w3.org/tr/xhtml1/dtd/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="content-type" content="text/html; charset=utf-8" /> <title>a simple form</title> </head> <body> <form name="getform" action="<?php echo htmlentities($_server['php_self']);?>" method="get"> 1) colors of u.s. flag?<br> <input type="radio" name="question1" value="a" />a) red, white , blue<br /> <input type="radio" name="question1" value="b" />b) yellow, red , blue<br /> <input type="radio" name="question1" value="c" />c) blue, green , amber <br> <input type="submit" value="go" /><input type="reset" value="reset" /> </form> </body> </html> <? define(error, 'no answer input question. please select , answer.'); define(greeting, 'welcome simplest of php forms.'); $answer1=$_get['question1']; if(isset($_get["submit"])) { if($answer1=="a") {echo "you correct."; } elseif ($answer1=="") {echo error. "" ; } elseif ($answer1=="b" || $answer1=="c") {echo "your answer incorrect."; } } else {echo greeting. ""; } ?>
simple: you're using get, means when load page first time, it's via get, and
$answer1=$_get['question1']
will executed, making $answer1
null
, because there's no question1
parameter in url.
then comes up
elseif ($answer1=="")
and evaluates true, because standard ==
equality test, null == ""
true. boom, output bogus error message.
you avoid this, possibly, with
elseif ($answer1 === "")
note three =
signs, meaning strict equality test: data type , value must match.
that or switch form using post
submission, , can fence off whole form processing section with
if ($_server['request_method'] == 'post') { ... process form here }
Comments
Post a Comment