jhodara Posted November 23, 2010 Share Posted November 23, 2010 Hi, I am new to php, and I have run into a problem. The tutorial I am using has provided me with this exact code. But it does not work for me. Its very simple. Here is the HTML page: <body> <FORM ACTION="welcome.php" METHOD=POST> First Name: <INPUT TYPE=TEXT NAME="name"> <INPUT TYPE=SUBMIT VALUE="GO"> </FORM> </body> And here is the php page: <body> <?php echo( "Welcome to our Web site, $name!" ); ?> </body> You can see the problem live at <http://www.freewaycreative.com/test> (dont mind the digits below) The name just does not show. Anyone know why? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/ Share on other sites More sharing options...
harristweed Posted November 23, 2010 Share Posted November 23, 2010 try <?php echo "Welcome to our Web site, $_POST['name']!" ; ?> Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138297 Share on other sites More sharing options...
jhodara Posted November 23, 2010 Author Share Posted November 23, 2010 Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138300 Share on other sites More sharing options...
jhodara Posted November 23, 2010 Author Share Posted November 23, 2010 Thanks Harristweed for the fix. But I took it one step further and put in an If/else conditional Heres the HTML <FORM ACTION="welcome.php" METHOD=POST> First Name: <INPUT TYPE=TEXT NAME="firstname"><BR> Last Name: <INPUT TYPE=TEXT NAME="lastname"> <INPUT TYPE=SUBMIT VALUE="GO"> </FORM> Heres the PHP: <?php if ( "Joe" == $firstname and "Shmo" == $lastname ) { echo( "Welcome, Myself!" ); } else { echo( "Welcome, $_POST[firstname] $_POST[lastname]!" ); } ?> When I enter Joe Shmo into the form, I want it to respond with, "Welcome, Myself". But it seems to skip the IF and go right to the ELSE. Anyone know what I am missing? I put this problem up live at http://www.freewaycreative.com/test Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138303 Share on other sites More sharing options...
nilansanjaya Posted November 23, 2010 Share Posted November 23, 2010 it should be <?php if ( $firstname == "Joe" && $lastname == "Shmo" ) { echo( "Welcome, Myself!" ); } else { echo( "Welcome, $_POST[firstname] $_POST[lastname]!" ); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138308 Share on other sites More sharing options...
jhodara Posted November 23, 2010 Author Share Posted November 23, 2010 both "and" and "&&" work the same in PHP. Here was what I had to change <?php if ( $_POST[firstname] == "Joe" && $_POST[lastname] == "Shmo") { echo( "Welcome, Myself!" ); } else { echo( "Welcome, $_POST[firstname] $_POST[lastname]!" ); } ?> the PHP did not recognize $firstname and $lastname Thank you, though. Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138310 Share on other sites More sharing options...
Rifts Posted November 23, 2010 Share Posted November 23, 2010 echo( "Welcome, $_POST[firstname] $_POST[lastname]!" ); what an "!" do if its after like above? Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138471 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 Since the ! is inside of quotes, it will echo. Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138494 Share on other sites More sharing options...
Pikachu2000 Posted November 23, 2010 Share Posted November 23, 2010 both "and" and "&&" work the same in PHP. Here was what I had to change <?php if ( $_POST[firstname] == "Joe" && $_POST[lastname] == "Shmo") { echo( "Welcome, Myself!" ); } else { echo( "Welcome, $_POST[firstname] $_POST[lastname]!" ); } ?> the PHP did not recognize $firstname and $lastname Thank you, though. If you had error reporting on, you'd be getting warnings about "undefined constant"s in that script. Whenever you use an associative array element, the index should be in quotes: $_POST['firstname'] (numeric array indices don't get quoted, however). There is an exception when it is used in a quoted string: $string = "This is an array element: $_POST[firstname]."; but for consistency and clarity, most people either concatenate it, or enclose it in curly braces: $string = "This is an array element: {$_POST['firstname']}."; // or // $string = "This is an array element " . $_POST['firstname'] . "."; Quote Link to comment https://forums.phpfreaks.com/topic/219551-very-simple-form-using-php-not-working-please-help/#findComment-1138504 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.