php表单和验证 - 表单值返回为空

时间:2018-01-19 19:01:44

标签: php mysql validation html-form

我希望有人可以对这个问题有所了解。我创建了一个注册表单,提交到第二页进行验证。有各种检查来捕获错误和不需要的用户输入,并且无法检查空字段..即使所有字段都包含数据,它仍然被返回为空

这是我的表单 - 注意它确实包含一个隐藏字段,建议使用/>而不是通常的>标签 - 两种方式没有区别

<form id="registersocial" class="SRF" name="registersocial" action="php.includes/rasocial.inc.php" method="POST">

    <div id="formheaders"><strong>Personal Details</strong></div>
    <br/>
    <fieldset>
        <lable><strong>First Name</strong><br/>
                <input type="text" name="firstname" id="firstname" class="SRF" onKeyup="restrict('firstname')" placeholder="First Name" >
                <span id="Errmsg-first"></span>
            </lable>
            <br/>
            <lable><strong>Last Name</strong><br/>
                <input type="text" name="lastname" id="lastname" class="SRF" onKeyup="restrict('lastname')" placeholder="Last Name" >
                <span id="Errmsg-last"></span>
            </lable>
            <br/>
        </fieldset>
        <fieldset>
            <lable><strong>Date of Birth</strong><br/>
                <select name="birthmonth" id="birthmonth" class="SRF">
                    <?php require("php.includes/month.inc.php"); ?>
                </select> 
                <span id="Errmsg-mob"></span>

                <input type="text" name="birthday" id="birthday" class="SRF" maxlength="2" placeholder="Day">
                <span id="Errmsg-dob"></span>

                <input type="text" name="birthyear" id="birthyear" class="SRF" maxlength="4" placeholder="Year">
                <span id="Errmsg-yob"></span>
            </lable>
            <br/>
        </fieldset>
        <fieldset>
            <lable><strong>Location</strong><br/>
                <select name="country" id="country">
                    <?php require("php.includes/countrylist.php"); ?>
                </select>
                <span id="Errmsg-country"></span>
            </lable>
            <br/>
    </fieldset>

    <hr class="SRF">

    <div id="formheaders"><strong >Account Information</strong></div>
    <br/>
    <fieldset>

                <input type="hidden" name="accounttype" id="accounttype" class="SRF" value="Social"/>

            <lable><strong>Create a Username</strong><br/>
                <input type="text" name="username" id="username" class="SRF" onKeyup="restrict('username')" onblur="checkusername()" placeholder="Username" >
                <span id="Errmsg-username"></span>
            </lable>
            <br/>
            <lable><strong>Your Current Email</strong><br/>
                <input type="email" name="email" id="email" class="SRF" onKeyup="restrict('email')" placeholder="Your Email" >
                <span id="Errmsg-email"></span>
            </lable>
            <br/>
            <lable><strong>Create a Password</strong><br/>
                <input type="password" name="pwd" id="pwd" class="SRF" placeholder="Password" >
                <span id="Errmsg-password"></span>
            </lable>
            <br/>
            <br/>
            <input type="submit" id="submit" name="submit" value="submit">

        </fieldset>
        <br/>
        <span id="status"></span>
        <br/>

</form>

下面是获取发布数据并通过验证文件名运行的文件是rasocial.inc.php - 再次我的问题是,在完成表单后,我在网址中收到一个空错误 - 我相信它是简单但却看不到我的生活

<?php
if(isset($_POST['submit']) && !empty($_POST['submit'])) {
include_once("ctb.inc.php");
$fn = mysqli_real_escape_string($pdo, $_POST['firstname']);
$ln = mysqli_real_escape_string($pdo, $_POST['lastname']);
$bm = mysqli_real_escape_string($pdo, $_POST['birthmonth']);
$bd = mysqli_real_escape_string($pdo, $_POST['birthday']);
$by = mysqli_real_escape_string($pdo, $_POST['birthyear']);
$co = mysqli_real_escape_string($pdo, $_POST['country']);
$at = mysqli_real_escape_string($pdo, $_POST['accounttype']);
$un = mysqli_real_escape_string($pdo, $_POST['username']);
$em = mysqli_real_escape_string($pdo, $_POST['email']);
$pwd = mysqli_real_escape_string($pdo, $_POST['pwd']);
var_dump($fn, $ln, $bm, $bd, $by, $co, $at, $un, $em, $pwd);
//Error Handlers
//Check for empty fields
if (empty($fn) || empty($ln) || empty($bm) || empty($bd) || empty($by) || empty($co) || empty($at) || empty($un) || empty($em) || empty($pwd)) {
    header("Location: ../registersocial.php?registersocial=empty");
    exit();
} else {
    //Check firstname and lastname for valid chars
    if (!preg_match("/^[a-zA-Z]*$/", $fn) || !preg_match("/^[a-zA-Z]*$/", $ln)) {
        header("Location: ../registersocial.php?registersocial=invalidcharacters");
        exit();
    } else {
        //Check birth month has been selected
        if ($_POST['birthmonth'] == '0') {
                header("Location: ../registersocial.php?registersocial=birthmonth");
                exit();
            } else {
            //Check birth day is numbers only
            if (!preg_match("/^[0-9]*$/", $bd)) {
                        header("Location: ../registersocial.php?registersocial=birthday");
                        exit();
            } else {
                //Check the birth day length is 2 characters
                if (strlen($bd) != 2 ) {
                                header("Location: ../registersocial.php?registersocial=birthdaylength");
                                exit();
                } else {
                    //Check birth year is numbers only
                    if (!preg_match("/^[0-9]*$/", $by)) {
                                        header("Location: ../registersocial.php?registersocial=birthyear");
                                        exit();
                    } else {
                        //Check birth year is 4 characters
                        if (strlen($by) != 4 ) {
                                                header("Location: ../registersocial.php?registersocial=birthyearlength");
                                                exit();
                        } else {
                            //Check country has been selected
                            if ($_POST['country'] == '0') {
                                                    header("Location: ../registersocial.php?registersocial=country");
                                                    exit();
                            } else {
                                //Check if accounttype has been modified
                                if (!preg_match("/^[a-zA-Z]*$/", $at) || $_POST['accounttype'] != 'Social') {
                                    header("Location: ../registersocial.php?registersocial=accounttype");
                                    exit();
                                } else {
                                    //Check username isnt taken
                                    if (!preg_match("/^[a-zA-Z0-9]*$", $un)) {
                                        header("Location: ../registersocial.php?registersocial=invalidusername");
                                        exit();
                                    } else {
                                        //Check username is not taken in db
                                        $stmt = $pdo->prepare('SELECT * FROM sh_userdata WHERE username =?');
                                        $stmt->execute($un);
                                        $usernamecheck = $stmt->fetch();
                                        if ($usernamecheck > 0 ) {
                                            header("Location: ../registersocial.php?registersocial=usernametaken");
                                            exit();
                                        } else {
                                            //Check email is valid
                                            if (!filter_var($em, FILTER_VALIDATE_EMAIL) ) {
                                                header("Location: ../registersocial.php?registersocial=invalidemail");
                                                exit();
                                            } else {
                                                //Check if email exists in db
                                                $stmt = $pdo->prepare('SELECT * FROM sh_userdata WHERE email =?');
                                                $stmt->execute($em);
                                                $emailcheck = $stmt->fetch();
                                                if ($emailcheck > 0 ) {
                                                    header("Location: ../registersocial.php?registersocial=emailtaken");
                                                    exit();
                                                    //add dob fields to make date of birth
                                                    $dob = new DateTime($by.'-'.$bm.'-'.$bd);
                                                    $dob->format('Y-m-d');
                                                    //hash password
                                                    $hashedpwd = password_hash($pwd, PASSWORD_DEFAULT);
                                                    //insert user into db
                                                    $stmt = $pdo->prepare("INSERT INTO sh_userdata (username, email, password, accounttype, signupdate, lastlogindate) VALUES (?,?,?,?,NOW(),NOW())");
                                                    $stmt->execute(array("$un","$em","$hashedpwd","$at"));
                                                    header("Location: ../registersocial.php?registersocial=sucess");
                                                    exit();
                                                } 
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }
    }
}   
} else {
header("Location: ../registersocial.php?registersocial=nopost");
exit();
}

任何帮助或建议都会非常感激

0 个答案:

没有答案