0

im tring to call php page from a simple html page, and i cant find the syntax error this is the html

<html>
<body style="background-color:#990000;">
<h5 align="center" style="font-family:tahoma;color:white;font-size:50px;"> Welcome! </h5> 

<form align="center" method="post" action="php_site.php"> 
First name: <input type="text" name="firstname"><br>
Last name: <input type="text" name="lastname"><br>
<input type="submit" value="Search!">
</form>
</body>
</html>

and this is the php

<?php   
if (isset($_POST['firstname']) && (isset($_POST['lastname'])))
{
    echo "Welcome $_POST['firstname']";
    echo "This is your last name $_POST['lastname']";  
}
else 
{
    echo "This is Empty!";
}
?>

thanks!

6
  • 2
    Try echo "Welcome" . $firstname; instead. Commented Nov 30, 2013 at 15:56
  • 1
    @Fred-ii-: Seriously? Rely on register_globals? Commented Nov 30, 2013 at 15:57
  • Was just a suggestion. That's what the comments box is for. It's up to the OP to test out the possibilities and "try" something. Not a points monger ;-) Commented Nov 30, 2013 at 15:58
  • @Fred-ii-: Right. So I suggest you leave the keys to your house on the door. It's up to you to test if that might be a problem. Commented Nov 30, 2013 at 16:00
  • My door is always open @Jon ;-) My "house" door that is. I ain't afraid of the big bad wolf hehehe Commented Nov 30, 2013 at 16:01

4 Answers 4

4

The syntax of the embedded references to $_POST inside the strings is not quite right. Some ways to fix it are:

  1. Wrap them in {} brackets:

    echo "Welcome {$_POST['firstname']}";
    echo "This is your last name {$_POST['lastname']}";
    
  2. Or, remove the single quotes:

    echo "Welcome $_POST[firstname]";
    echo "This is your last name $_POST[lastname]";
    
  3. Or, use string concatentation with . instead of embedding:

    echo "Welcome " . $_POST['firstname'];
    echo "This is your last name " . $_POST['lastname'];
    
  4. And/or, pull the values into plain-named variables first:

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    echo "Welcome $firstname";
    echo "This is your last name $lastname";
    

See the doc on variable parsing in strings.

Sign up to request clarification or add additional context in comments.

1 Comment

+1, or lose the single quotes around the keys. But concatenation is IMO the best.
1

Try this:

<?php   
if (isset($_POST['firstname']) && isset($_POST['lastname']))
{
    echo "Welcome ".$_POST['firstname'];
    echo "This is your last name ".$_POST['lastname'];  
}
else 
{
    echo "This is Empty!";
}
?>

Comments

0
<?php   
    if (isset($_POST['firstname']) && isset($_POST['lastname']) )
    {
        echo "Welcome ".$_POST['firstname'];
        echo "This is your last name $_POST['lastname']";  
    }
    else 
    {
        echo "This is Empty!";
    }
?>

Comments

0

First off, you should set your set your $_POST values to variables.

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];

Then, ensure that you're splitting the strings from the variables by using the concatenation punctuation .:

echo "Welcome" . $firstname; instead of echo "Welcome $_POST['firstname']";

Also, please post the syntax error you're getting. If PHP is breaking (white screen of death), then please add ini_set("display_errors", "1"); in order to write errors to the screen, and then post the error output.

2 Comments

Unrestrained import of all $_POST variables into the global scope is a bad idea. It's taken years to make it go away. Don't bring it back.
No, fair enough - I think I need some more coffee, updated the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.