简体   繁体   English

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

[英]Can you tell me what's wrong with this PHP form?

I'm trying to make a simple website in which I ask the user for their name and DOB in order to output "Hello (name) you are (age) years old and you will graduate in (Graduation Date). I feel like my code is right but when i submit the form it leads me to a blank page.我正在尝试制作一个简单的网站,在该网站中我询问用户他们的姓名和出生日期,以便输出“你好(姓名)你是(年龄)岁,你将毕业(毕业日期)。我觉得我的代码是正确的,但是当我提交表单时,它会将我带到一个空白页面。

My code: HTML我的代码: 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:

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

Sorry if I presented this wrong I'm new to this website.对不起,如果我提出这个错误,我是这个网站的新手。

your tests are wrong.你的测试是错误的。 Here is a corrected code:这是一个更正的代码:

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

I think我认为

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

that is not working very well.这不是很好。 You should check every variable with empty or isset .您应该检查每个变量为emptyisset With one = you give the variable a value in your case nothing.使用 one =在你的情况下,你给变量一个值。

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

1) you need to use == in ifs ... a single = works differently 1) 你需要在 ifs 中使用 == ......单个 = 的工作方式不同
( this way, you just try to check, if you can assign the value of $year to $name ) (这样,您只需尝试检查,是否可以将 $year 的值分配给 $name )

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

2) you need to rewrite that if 2)你需要重写,如果

if( ( strlen( $name ) == 0 ) && ( strlen( $year ) == 0 ) .....

3) yo need to rewrite your elseif stuff as well 3)你也需要重写你的elseif东西

Try this:尝试这个:

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

}
?>

if this does not work, edit your question and add the HTML form code that you have written如果这不起作用,请编辑您的问题并添加您编写的 HTML 表单代码

do not use assign operator instead of compression operator.不要使用赋值运算符代替压缩运算符。 in php在 php 中

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

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM