Jump to content

Very simple Form using PHP not working, please help.


jhodara

Recommended Posts

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!

 

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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'] . ".";

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.