我的php显示验证错误,但JS不是

时间:2015-09-17 13:45:59

标签: php validation

我正处于为我的网站制作注册页面的早期阶段。但是,我创建的基本表单正在通过javascript和php验证,以确保输入正确的数据。即使javascript没有显示任何错误并允许表单提交,PHP错误仍然会被标记并停止。下面是php和html表单的代码。任何帮助将不胜感激,这些事情通常比预期容易得多,但它让我疯狂,因为它没有显示任何语法错误,只是我为用户设置的错误。 包含文件只有mysql密码和一些用于检查电话号码的基本功能。 提前致谢 HTML

<?php require_once("functions.inc"); ?>

<head>
     <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
    <script src="register.js"></script>
    <link rel="stylesheet" href="form.css">
    <title>A Form</title>
</head>

<body>

    <form id="userForm" method="POST" action="register-process.php">
        <div>
            <fieldset>
                <legend>Registration Information</legend>
                <div id="errorDiv">
                    <?php 
                        if (isset($_SESSION['error']) && isset($_SESSION['formAttempt'])){
                            unset($_SESSION['formAttempt']);
                            print "errors encountered<br>\n";
                            foreach ($_SESSION['error'] as $error) {
                                print $error . "<br>\n";
                            }//end foreach
                        } // end if
                    ?>
                </div>

                 <label for="fname">First Name:* </label>
                <input type="text" id="fname" name="fname">
                <span class="errorFeedback errorSpan" id="fnameError">First Name is required</span>
                <br>

                 <label for="name">Last Name:* </label>
                <input type="text" id="lname" name="lname">
                <span class="errorFeedback errorSpan" id="lnameError">Last Name is required</span>
                <br>

                <label for="email">Email Address:* </label>
                <input type="text" id="email" name="email">
                <span class="errorFeedback errorSpan" id="emailError">Email is required</span>
                <br>

                <label for="password1">Password:* </label>
                <input type="password" id="password1" name="password1">
                <span class="errorFeedback errorSpan" id="password1Error">Password is required</span>
                <br>

                <label for="password2">Varify Password:* </label>
                <input type="password" id="password2" name="password2">
                <span class="errorFeedback errorSpan" id="password2Error">Password's do not match</span>
                <br>

                <label for="addr">Address: </label>
                <input type="text" id="addr" name="addr">
                <br>

                <label for="city">City: </label>
                <input type="text" id="city" name="city">
                <br>

                <label for="state">State: </label>
                <select name="state" id="state">
                    <option></option>
                    <option value="AL">Alabama</option>
                    <option value="CA">California</option>
                    <option value="CO">Colorado</option>
                    <option value="FL">Florida</option>
                    <option value="IL">Illinois</option>
                    <option value="NJ">New Jersey</option>
                    <option value="NY">New York</option>
                    <option value="WI">Winconsin</option>
                </select>
                <br>

                <label for="zip">ZIP: </label>
                <input type="text" id="zip" name="zip">
                <br>

                <label for="phone">Phone Number: </label>
                <input type="text" id="phone" name="phone">
                <span class="errorFeedback errorSpan" id="phoneError">Format: xxx-xxx-xxxx</span>
                <br>
                <br>

                <label for="work">Number Type:</label>
                <input class="radioButton" type="radio" name="phoneType" id="work" value="work">
                <label class="radioButton" for="work">Work</label>
                <input class="radioButton" type="radio" name="phoneType" id="home" value="home">
                <label class="radioButton" for="home">Home</label>
                <span class="errorFeedback errorSpan phoneTypeError" id="phoneTypeError">Please Choose an option.</span>
                <br>

                <input type="submit" id="submit" name="submit">
            </fieldset>
        </div>
    </form>

</body>

PHP注册process.php

    <?php

require_once('functions.inc');

//prevent access if they havent submitted the form!!
if (!isset($_POST['submit'])) {
    die(header("location: register.php"));
}

$_SESSION['formAttempt'] = true;

if (isset($_SESSION['error'])) {
    unset($_SESSION['error']);
}

$_SESSION['error'] = array();

$required = array("fname","lname", "email", "password1", "password2");                   

//check required fields!
foreach ($required as $requiredField) {
    if (!isset($_POST[requiredField]) || $_POST[$requiredField] == "") {
        $_SESSION['error'][] = $requiredField . " is required.";
    }
}

if (!preg_match('/^[\w .]+$/',$_POST['fname'])) {
    $_SESSION['error'][] = "Name must be letters and numbers only.";
}
if (!preg_match('/^[\w .]+$/',$_POST['lname'])) {
    $_SESSION['error'][] = "Name must be letters and numbers only.";
}

if (isset($_POST['state']) && $_POST['state'] != "") {
    if (!isValidState($_POST['state'])) {
        $_SESSION['error'][] = "Please choose a valid state";
    }
}

if (isset($_POST['zip']) && $_POST['zip'] != "") {
    if (!isValidZip($_POST['zip'])) {
        $_SESSION['error'][] = "ZIP code error";
    }
}


if (isset($_POST['phone']) && $_POST['phone'] != "") {
    if (!preg_match('/^[\d]+$/', $_POST['phone'])) {
        $_SESSION['error'][] = "Phone numbner should be digits only.";
    } else if (strlen($_POST['phone']) < 10 ) {
        $_SESSION['error'] = "Phone number should be at least 10 digits.";
    }
    if (!isset($_POST['phoneType']) || $_POST['phoneType'] == "") {
        $_SESSION['error'][] = "Please choose a phone type.";
    } else {
        $validPhoneTypes = array("work","home");
        if (!in_array($_POST['phoneType'], $validPhoneTypes)) {
            $_SESSION['error'][] = "Please choose a valid phone type";
        }
    }
}

if (!filter_var($_POST['email'],FILTER_VALIDATE_URL)) {
    $_SESSION['error'][] = "Invalid e-mail address!";
}

if ($_POST['password1'] != $_POST['password2']) {
    $_SESSION['error'] = "Passwords do not match";
}

//Final Disposition
if (count($_SESSION['error']) > 0) {
    die (header("Location: register.php"));
} else {
    if (registerUser($_POST)) {
        unset($_SESSION['formAttempt']);
        die(header("Location: success.php"));
    } else {
        error_log("problem registering user: {$_POST['email']}");
        $_SESSION['error'][] = "Problem registering account";
        die(header("Location: register.php"));
    }
}

扩展名是进程php文件的其余部分,我已经评论了错误的来源....再次感谢..

if (count($_SESSION['error']) > 0) {
    die (header("Location: register.php"));
} else {
    if (registerUser($_POST)) {
        unset($_SESSION['formAttempt']);
        die(header("Location: success.php"));
    } else {
        error_log("problem registering user: {$_POST['email']}"); // THIS IS WHERE THE ERROR IS COMNING FROM
        $_SESSION['error'][] = "Problem registering account";
        die(header("Location: register.php"));
    }
}

function registerUser($userData) {
    $mysqli = new mysqli(DBHOST,DBUSER,DBPASS,DB);
    if ($mysqli->connect_errno) {
        error_log("Cannot connect to MySQL: " . $mysqli->connect_error);
        return false;
    }
    $email = $mysqli->real_escape_string($_POST['email']);
    //Check for an existing user
    $findUser = "SELECT id from Customer where email = '{$email}'";
    $findResult = $mysqli->query($findUser);
    $findRow = $findResult->fetch_assoc();
    if (isset($findRow['id']) && $findRow['id'] != "") {
        $_SESSION['error'][] = "A user with that email already exists";
        return false;
    }

    $lastname = $mysqli->real_escape_string($_POST['lname']);
    $firstname = $mysqli->real_escape_string($_POST['fname']);

    $cryptedPassword = crypt($_POST['password1']);
    $password = $mysqli->real_escape_string($cryptedPassword);

    if (isset($_POST['addr'])) {
        $street = $mysqli->real_escape_string($_POST['addr']);
    } else {
        $street = "";
    }

    if (isset($_POST['city'])) {
        $city = $mysqli->real_escape_string($_POST['city']);
    } else {
        $city = "";
    }

    if (isset($_POST['state'])) {
        $state = $mysqli->real_escape_string($_POST['state']);
    } else {
        $state = "";
    }

    if (isset($_POST['zip'])) {
        $zip = $mysqli->real_escape_string($_POST['zip']);
    } else {
        $zip = "";
    }

    if (isset($_POST['phone'])) {
        $phone = $mysqli->real_escape_string($_POST['phone']);
    } else {
        $phone = "";
    }

    if (isset($_POST['phoneType'])) {
        $phoneType = $mysqli->real_escape_string($_POST['phoneType']);
    } else {
        $phoneType = "";
    }

    $query = "INSERT INTO Customer (email,create_date,password,last_name,first_name,street,city,state,zip,phone,phone_type) " . "VALUES ('{$email}',NOW(),'{$password}','{$lastname}','{$firstname}'" . ",'{$street}','{$city}','{$zip}','{$phone}','{$phoneType}')";
    if ($mysqli->query($query)) {
        $id = $mysqli->insert_id;
        error_log("inserted {$email} as ID {$id}");
        return true;
    } else {
        error_log("Problem inserting {$query}");
                $_SESSION['error'][] = "HERE"; // THIS IS WHERE THE ERROR IS COMNING FROM
        return false;
    }
}
?>

1 个答案:

答案 0 :(得分:0)

您的查询中有错误。列数与值计数不同。你忘了传递$state

$query = "INSERT INTO Customer (email,create_date,password,last_name,first_name,street,city,state,zip,phone,phone_type) " . "VALUES ('{$email}',NOW(),'{$password}','{$lastname}','{$firstname}'" . ",'{$street}','{$city}','{$state}', '{$zip}','{$phone}','{$phoneType}')";
    if ($mysqli->query($query)) {
        $id = $mysqli->insert_id;
        error_log("inserted {$email} as ID {$id}");
        return true;
    } else {
        error_log("Problem inserting {$query}");
        error_log("Problem inserting {$mysqli->error}"); // log the error
        $_SESSION['error'][] = "HERE"; // THIS IS WHERE THE ERROR IS COMNING FROM
        return false;
    }