你能告诉我这个PHP表单有什么问题吗?

时间:2014-04-11 15:19:27

标签: php html forms

我试图创建一个简单的网站,我要求用户提供他们的姓名和DOB,以便输出"你好(姓名)你(年龄)岁,你将毕业(毕业日期)。我觉得我的代码是正确的,但是当我提交表单时,它会将我引导到一个空白页面。

我的代码: 的 HTML

<body>
  <center>
    <form action="aform.php" method="post">
      Name:<input type="text" name="name" />
      <br />
      Date of Birth:
      <input type="text" size="4" placeholder="mm" name="month" /> 
      <input type="text" size="4" placeholder="dd" name="day" />
      <input type="text" size="6" placeholder="yyyy" name="year" />
      <input type="submit" name="submit" value="Submit" />
      <br />
    </form>
  </center>
</body>

PHP:

<?php

if($_POST["submit"]){

    $name = $_POST["name"];
    $day = $_POST["day"];
    $month = $_POST["month"];
    $year = $_POST["year"];
    $datedisplay = date("yyyy");
    $birth = array($day,$month,$year);

    if($name = $year = $month = $day = ""){
        echo "You must fill out everything!";
    } else if($year = $month = $day > 0) {
        $yourage = 2014 - $year;
        $gradyear = $year + 22;
        $graddate = "6/TBA/$gradyear"; 
        echo "Hello" . $name . ", you are" . $yourage . "years old and you will graduate from college in" . $graddate ; 
    }   
}
?>

对不起,如果我提出这个错误,我是这个网站的新手。

5 个答案:

答案 0 :(得分:1)

你的测试是错误的。这是一个更正的代码:

<?php

if($_POST["submit"]) {

    $name = $_POST["name"];
    $day = $_POST["day"];
    $month = $_POST["month"];
    $year = $_POST["year"];
    $datedisplay = date("yyyy");
    $birth = array($day,$month,$year);

    if(empty($name) || empty($year) || empty($month) || empty($day)) {
        echo "You must fill out everything!";
    } else if($year > 0 && $month > 0 && $day > 0) {
        $yourage = 2014 - $year;
        $gradyear = $year + 22;
        $graddate = "6/TBA/$gradyear"; 
        echo "Hello" . $name . ", you are" . $yourage . "years old and you will graduate from college in" . $graddate ; 
    }   
}
?>

答案 1 :(得分:0)

我认为

if($name = $year = $month = $day = ""){
    echo "You must fill out everything!";
}

效果不佳。您应该使用 isset 检查每个变量。 使用一个 = ,您可以为您的变量赋一个值。

if(!empty($name) && !empty($year) && !empty($month)) {

答案 2 :(得分:0)

1)你需要在ifs中使用== ...单个=工作方式不同
(这样,你只需要检查,如果你可以将$ year的值分配给$ name)

if($name = $year = $month = $day = "")

2)如果

,你需要重写
if( ( strlen( $name ) == 0 ) && ( strlen( $year ) == 0 ) .....

3)你需要重写你的elseif东西

答案 3 :(得分:-1)

试试这个:

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST'){

$name = $_POST["name"];
$day = $_POST["day"];
$month = $_POST["month"];
$year = $_POST["year"];
$datedisplay = date("yyyy");
$birth = array($day,$month,$year);

    if($name == "" || $year == "" || $month == "" || $day == ""){
    echo "You must fill out everything!";
    }

    else if($year > 0 && $month > 0 && $day > 0){
    $yourage = 2014 - $year;
    $gradyear = $year + 22;
    $graddate = "6/TBA/$gradyear"; 
    echo "Hello" . $name . ", you are" . $yourage . "years old and you will graduate from college in" . $graddate ; 
    }

}
?>

如果这不起作用,请编辑您的问题并添加您编写的HTML表单代码

答案 4 :(得分:-1)

不要使用assign运算符而不是压缩运算符。在PHP

if($name = $year = $month = $day = ""){

}   
else if($year = $month = $day > 0){}