PHP和IF语句

时间:2013-11-30 15:55:12

标签: php

从一个简单的html页面调用php页面,我无法找到语法错​​误 这是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>

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

谢谢!

4 个答案:

答案 0 :(得分:4)

字符串中对$ _POST的嵌入式引用的语法不太对。解决问题的一些方法是:

  1. 将它们包裹在{}括号中:

    echo "Welcome {$_POST['firstname']}";
    echo "This is your last name {$_POST['lastname']}";
    
  2. 或者,删除单引号:

    echo "Welcome $_POST[firstname]";
    echo "This is your last name $_POST[lastname]";
    
  3. 或者,使用字符串连续与.而不是嵌入:

    echo "Welcome " . $_POST['firstname'];
    echo "This is your last name " . $_POST['lastname'];
    
  4. 和/或,首先将值拉入普通命名变量:

    $firstname = $_POST['firstname'];
    $lastname = $_POST['lastname'];
    echo "Welcome $firstname";
    echo "This is your last name $lastname";
    
  5. 请参阅variable parsing in strings上的文档。

答案 1 :(得分:1)

试试这个:

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

答案 2 :(得分: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!";
    }
?>

答案 3 :(得分:0)

首先,您应该将$_POST值设置为变量。

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

然后,确保使用串联标点.从变量中拆分字符串:

echo "Welcome" . $firstname;代替echo "Welcome $_POST['firstname']";

另外,请发布您获得的语法错误。如果PHP破解(白屏死机),请添加ini_set("display_errors", "1");以便将错误写入屏幕,然后发布错误输出。