如果HTML表单的action属性中的else语句可以使用php处理表单吗?

时间:2018-10-24 15:36:26

标签: php html forms validation action

我正在编写一个用于创建登录用户名和密码的表格。

如果帐户创建成功,那么我希望该用户进入实际的LOGIN表单。

我创建了一系列检查,并传递了变量$errcheck,以便程序知道该怎么做。如果有错误,$errcheck将被设置为1。默认值为0

如果输入字段中有错误,则将再次显示帐户创建表单,如果一切正常,则会将用户详细信息插入表中,并将用户带到“登录”页面。

但是,每次将信息添加到表后,我只能让页面重新加载自身。我在表单的操作部分所做的事情是否被允许?我继续并包含了所有代码,以防对此有任何疑问。谢谢。

<!DOCTYPE html>
    <head>
        <style>
            .error {color: #FF0000;}
        </style>
    </head>
    <body>
    <?php 
        $busow_namef = $busow_namel= $owner_email = $bus_psswd = $psswd_confirm = "";                
        $busname_ERR = $busowname_ERR = $owneremail_ERR = $psswd_ERR =           
        $psswdconfirm_ERR =  "";
        $errcheck = 0;

        if ($_SERVER["REQUEST_METHOD"]=="POST") {
            //??????????????????? Check Login information ???????????????????
            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }

            if (empty($_POST["busow_namef"])) {
                $busowname_ERR = "Business owner's name is required";
                $errcheck = 1;
            } else {
                $busownamef = test_input($_POST["busow_namef"]);
            }

            if (empty($_POST["busow_namel"])) {
                $busowname_ERR = "business owner's name is required";
                $errcheck = 1;
            } else {
                $busownamel = test_input($_POST["busow_namel"]);
            }

            if (empty($_POST["bus_psswd"]))  {
                $psswd_ERR = "You must enter a password.";
                $errcheck = 1;
            } else if ((mb_strlen($_POST["bus_psswd"])) < 8) {
                $psswd_ERR = "The password must be 8-10 characters long and only include numbers and letters.";  
                $errcheck = 1;
            } else {
                $bus_psswd = test_input($_POST["bus_psswd"]);
            }

            if (empty($_POST["psswd_confirm"])) {
                $psswdconfirm_ERR = "Please confirm password.";
                $errcheck= 1;
            } else if  ($_POST["psswd_confirm"] != $_POST["bus_psswd"]) {
                $psswdconfirm_ERR = "The passwords do not match.";
                $errcheck = 1;
            } else {
                $psswd = test_input($_POST["psswd_confirm"]); 
                $h_psswd = password_hash($psswd, PASSWORD_DEFAULT);
            }

            if (empty($_POST["tandc"])) {
                $checktandc_ERR= "You must accept the terms and conditions.";
                $errcheck= 1;
            } else {
                $tandc = test_input($_POST["tandc"]);
            }

            if (empty($_POST["owner_email"])) {
                $owneremail_ERR = "Please enter an email address.";
                $errcheck = 1;
            } else { 
                 $_POST["owner_email"] = (filter_var($_POST["owner_email"], FILTER_SANITIZE_EMAIL));
            }

            if (filter_var($_POST["owner_email"] , FILTER_VALIDATE_EMAIL)){
                $owneremail = $_POST["owner_email"];
            } else {  
                $owneremail_ERR = "Please enter a valid email address.";
                $errcheck = 1;
            }

            //???????????????? Connect to database ??????????????????????????
            $link = mysqli_connect('domain', 'user', 'passwd'); 
            if (!$link) { 
                die('Could not connect: ' . mysqli_error()); 
            } 

            mysqli_select_db(database, $link); 

            if (!mysqli_select_db(louisville_ky1, $link)) {
                echo "database not selected";
            } else { 
                $sql = "SELECT owner_email FROM 3bus_owners WHERE owner_email = '$owneremail' ";
                $result = mysql_query($sql, $link);
                if (mysql_num_rows($result) > 0 ) {
                    $errcheck = 1;
                    $owneremail_ERR =  "This email is already registered. Please register with another address or click login.";
                } else {
                    $errcheck = 0;
                    $query = "INSERT INTO 3bus_owners (owner_email, h_psswd, busow_namef, busow_namel) VALUES ('$owneremail', '$h_psswd', '$busownamef',    
'$busownamel')";
                    $result2 = mysql_query($query, $link);
               } //end if num rows >0
           }//end connection check

       } // ???????????????????? end if server request method ????????????????
    ?>

    <!-- ~~~~~~~~~~~~~~~~~~~~~~~~Begin HTML FORM~~~~~~~~~~~~~~~~~~~~~~~~~ -->
    <h2>Create Business Login</h2>
    <br>
    <form method="post" action="<?php if ($errcheck = 1) { echo  
htmlspecialchars($_SERVER["PHP_SELF"]);
} else { echo 'ownersignin.php'; }?>"> 

        Business Owner's Name:<br> 
        First Name:<br><input type="text" name="busow_namef" value="<?php echo  
$busow_namef;?>">
        <span class="error">* <?php echo $busowname_ERR;?></span>
        <br>
        Last Name:<br><input type="text" name="busow_namel"value="<?php echo   
$busow_namel;?>">
        <span class="error">* <?php echo $busowname_ERR;?></span>
        <br>

        Business Owner's E-mail: *this will be your username for login and does not have to be posted in listing
        <br>
        <input type="text" name="owner_email" size="40"value="<?php echo  
$owner_email;?>">
        <span class="error">*<?php echo $owneremail_ERR;?></span> 
        <br><br>
        Password: <input type="password" name="bus_psswd" size="11" maxlength="10">
        <span class="error">*<?php echo $psswd_ERR;?></span> 
        <br>
        Confirm Password: <input type="password" name="psswd_confirm" size="11" maxlength="10">
        <span class="error">*<?php echo $psswdconfirm_ERR;?></span> 
        <br>
        <br>
        <input type="checkbox" name="tandc">I have read and accept the 
        <a href="/termsandconditions.php" target= "_blank">Terms and 
Conditions</a>.
        <span class="error">*<?php echo $checktandc_ERR;?></span>
        <br>
        <br>
        <input type="submit" name="submit" value="Create Login">
    </form>
</body>

2 个答案:

答案 0 :(得分:0)

上方的

snippit:

<form method="post" action="<?php if ($errcheck = 1) { echo htmlspecialchars($_SERVER["PHP_SELF"]); } else { echo 'ownersignin.php'; }?>"> 

我从未见过这样写过的表单动作属性,但是...尝试将“ PHP_SELF”周围的“双引号”更改为单引号:$ _SERVER ['PHP_SELF']。那可能会引起问题,因为它可能被解释为:

action="<?php if ($errcheck = 1) { echo htmlspecialchars($_SERVER["

然后,验证该代码示例是否来自以下页面:“ ownersignin.php”。听起来好像是此页面的名称,而不是表单将重定向到的页面的名称。

echo 'ownersignin.php';

如果这是您的代码所在页面的名称,则它将无限循环地发送给您。

答案 1 :(得分:0)

注册成功后,您不应重新打印注册表格。而是将用户重定向到登录表单。

在完成所有验证检查后,请执行以下操作:

if (!$errcheck) {
    header("Location: ownersignup.php");
    exit;
}
?>
<!-- ~~~~~~~~~~~~~~~~~~~~~~~~Begin HTML FORM~~~~~~~~~~~~~~~~~~~~~~~~~ -->
<h2>Create Business Login</h2>
<br>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>"> 
...