phpagent Posted August 13, 2011 Share Posted August 13, 2011 Hi all, New at the forum but i am freak for php but i have this strage problem with code.. I'm sure that it should work but i can't see whats missing part in my if statement. Can't be simpler that this, any idea? <html> <head> <title><?php echo "Form Test";?></title> <?PHP $City = $_POST['City']; if ($City = "New York") { print ("Cool city!"); } else { print ("Write some other city"); } ?> </head> <body> <FORM NAME ="City Searcher" METHOD ="pOST" ACTION = ""> <INPUT TYPE = "Enter city" VALUE ="City" NAME = "City"> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search"> </FORM> </body> </html> Quote Link to comment https://forums.phpfreaks.com/topic/244691-if-statment-doesnt-work/ Share on other sites More sharing options...
Alex Posted August 13, 2011 Share Posted August 13, 2011 You need to use the comparison operator, ==. All you're doing is setting $City equal to "New York". Quote Link to comment https://forums.phpfreaks.com/topic/244691-if-statment-doesnt-work/#findComment-1256809 Share on other sites More sharing options...
skwap Posted August 13, 2011 Share Posted August 13, 2011 I modified your code & now its working... <?php echo '<html> <head> <title>Form Test</title>'; if($_SERVER['REQUEST_METHOD']=='POST') { $City = $_POST['City']; if ($City == "New York") { echo "Cool city!"; } else { echo "Write some other city"; } } echo '</head> <body> <FORM NAME ="City Searcher" METHOD ="POST" ACTION = ""> <INPUT TYPE = "TEXT" NAME = "City" VALUE = "City"> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search"> </FORM> </body> </html>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/244691-if-statment-doesnt-work/#findComment-1256812 Share on other sites More sharing options...
phpagent Posted August 13, 2011 Author Share Posted August 13, 2011 omg i have done the = = and that is why it didn't work all because one space.. lol tnx Alex P.S Also tnx to skwap for som upgrade on code i will check it out Quote Link to comment https://forums.phpfreaks.com/topic/244691-if-statment-doesnt-work/#findComment-1256813 Share on other sites More sharing options...
jcbones Posted August 13, 2011 Share Posted August 13, 2011 Just for fun: <?php $content = <<<'NOWDOC' <html> <head> <title>Form Test</title> NOWDOC; if(isset($_POST['Submit1'])) { $city = (!empty($_POST['City']) && strtolower($_POST['City']) == 'new york') ? 'Cool City!' : 'Try Another City'; } $content = <<<HEREDOC </head> <body> <FORM NAME ="City Searcher" METHOD ="POST" ACTION = ""> <INPUT TYPE = "TEXT" NAME = "City" VALUE = "City"> <INPUT TYPE = "Submit" Name = "Submit1" VALUE = "Search"> </FORM> <br /><span style="color:red;font-size:200%">{$city}</span> </body> </html> HEREDOC; echo $content; ?> Only works in >= PHP5.3.0 Quote Link to comment https://forums.phpfreaks.com/topic/244691-if-statment-doesnt-work/#findComment-1256817 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.