Jonatan
Jonatan

Reputation: 29

PHP AND IF statement

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!

Upvotes: 0

Views: 92

Answers (4)

Xarus
Xarus

Reputation: 6999

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.

Upvotes: 0

Boann
Boann

Reputation: 50021

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.

Upvotes: 4

131
131

Reputation: 1385

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!";
}
?>

Upvotes: 1

Pain67
Pain67

Reputation: 336

<?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!";
    }
?>

Upvotes: 0

Related Questions