php echo在if else语句中不起作用

时间:2015-04-06 22:00:08

标签: php html if-statement echo registration

我试图添加die(“密码不匹配”);还有回声。但是,当点击注册时,这似乎仍然没有做任何事情。变量名称与联系表单中的变量名称完全匹配。我只能想到一件事。可能听起来很有趣它因为我已经登录(注册)了?我很抱歉,如果这个问题得到了解答,但我没有看到任何答案能帮助我处理类似情况的人。干杯!

<?php
require('inc/phpfunctions.php');

if(isset($_POST['submit'])){

//perform the verification
$email1 = $_POST['email1'];
$email2 = $_POST['email2'];
$pass1 = $_POST['pass1'];
$pass2 = $_POST['pass2'];

if ($email1 == $email2){
    if($pass1 == $pass2){
    //All good. Carry On.
    }else{
        echo "Sorry your passwords do not match. <br />";
        exit();
     }
    }else{
        echo "Sorry your email's do not match<br /><br />";
}



 }else{

 $form = <<<EOT

 <form action="register.php" method="POST">
 First Name: <input type= "text" name="name" /><br />
 Last Name: <input type= "text" name="lname" /><br />
 Username: <input type= "text" name="uname" /><br />
 Email: <input type= "text" name="email1" /><br />
 Confirm Email: <input type= "text" name="email2" /><br />
 Password: <input type= "password" name="pass1" /><br />
 Confirm Password: <input type= "password" name="pass2" /><br />
 <input type="submit" value="Register" name"submit" />
 </form>
EOT;

echo $form;

 }

?>

1 个答案:

答案 0 :(得分:1)

根据您的代码,您的主要问题是您在表单name="submit"中缺少等号。以下是对符号的重组:

// Establish fail by default
$success    =   false;
if(isset($_POST['submit'])){

        // Create a function that will match input values
        function MatchVars($var1,$var2)
            {
                if(empty($var1) || empty($var2))
                    return 0;

                return ($var1 == $var2)? 1 : 0;
            }

        // Store error/success
        $validate['email']  =   MatchVars($_POST['email1'],$_POST['email2']);
        $validate['pass']   =   MatchVars($_POST['pass1'],$_POST['pass2']);
        // Check each for individual error
        if($validate['email'] != 1)
            echo 'Emails do not match!';
        if($validate['pass'] != 1)
            echo 'Passwords do not match!';

        // Assign success or fail based on the sum of the validate array
        // Less than 2 is fail. This will overwrite the first established
        // instance of the $success variable
        $success    =   (array_sum($validate) == 2)? true:false;
    }

if($success == false) {
        // the name="submit" is missing and equal sign
        // Since you are basing your forward movement on that variable
        // if it's "broken," the if/else will not work
        $form = <<<EOT
<form action="" method="POST">
First Name: <input type= "text" name="name" /><br />
Last Name: <input type= "text" name="lname" /><br />
Username: <input type= "text" name="uname" /><br />
Email: <input type= "text" name="email1" /><br />
Confirm Email: <input type= "text" name="email2" /><br />
Password: <input type= "password" name="pass1" /><br />
Confirm Password: <input type= "password" name="pass2" /><br />
<input type="submit" value="Register" name="submit" />
</form>
EOT;

        echo $form; 
    }