使用ajax、php和javascript在验证通过后插入数据库

使用ajax、php和javascript在验证通过后插入数据库,javascript,php,jquery,mysql,ajax,Javascript,Php,Jquery,Mysql,Ajax,这是my signUp.php我不知道这里有什么错误,它阻止我的表单值被插入数据库 <?php $errors = array(); $data = array(); // validate the variables ====================================================== // if any of these variables don't exist, add an error to our $errors array

这是my signUp.php我不知道这里有什么错误,它阻止我的表单值被插入数据库

<?php
$errors = array();
$data = array();


// validate the variables ======================================================
// if any of these variables don't exist, add an error to our $errors array


    if(empty($_POST["full_Name"])){
        $errors['full_Name'] = "Please fill in yor full name";
    }
    else{
        $full_Name = test_Inputs($_POST['full_Name']);
        //using regular expression to check if the name includes only letters and whitespaces
        if(@!preg_match("/^[A-z\s]*$/", $full_Name)){
            $errors['full_Name'] = "Only alphabets and whitespace";
        }
    }
    if(empty($_POST['user_phoneNumber'])){
        $errors['user_phoneNumber'] = "Mobile number is required";
    }
    else{
        $mobileNumber = test_Inputs($_POST['user_phoneNumber']);
        // using regex to make sure only numbers are inputted in the field
        if(@!preg_match("/^[0-9]+$/", $mobileNumber)){
            $errors['user_phoneNumber'] = "Only numbers allowed";
        }
    }
    if(empty($_POST['user_Email'])){
        $errors['user_Email'] = "Please fill in your email address";
    }
    else{
        $Email = test_Inputs($_POST['user_Email']);
        //using regex to validate email input
        if(@!preg_match("/^[^0-9][A-z0-9_]+([.][A-z0-9_]+)*[@][A-z0-9_]+([.][A-z0-9_]+)*[.][A-z]{2,4}$/", $Email)){
            $errors['user_Email'] = "Invalid Email address";
        }
    }
    if(empty($_POST['userName'])){
        $errors['userName'] = "User name field is blank";
    }
    else{
        $userName = test_Inputs($_POST['userName']);
        if(@!preg_match("/^[A-z0-9]+$/", $userName)){
            $errors['userName'] = "Only letters and numbers allowed";
        }
    }
    if(empty($_POST['password'])){
        $errors['password'] = "Password field is blank";
    }
    else{
        $password = test_Inputs($_POST['password']);
        if(@!preg_match("/^[A-z0-9]+$/", $password)){
            $errors['password'] = "Only letters and numbers allowed";
        }
        if(strlen($password) < 8){
            $errors['password'] = "Password must be at least 8 characters long";
            $valid = false;
        }
    }
    if(empty($_POST['RPassword'])){
        $errors['RPassword'] = "Confirm your password";
        $valid = false;
    }
    else{
        $RPassword = test_Inputs($_POST['RPassword']);
        if($RPassword != $password){
            $errors['RPassword'] = "Passwords do not match";
        }
    }
    if(!isset($_POST['terms'])){
        $errors['terms'] = "Agree to the terms";
    }
    else {
        $Terms = test_Inputs($_POST['terms']);
    }
// if there are any errors in our errors array, return a success boolean of false
if ( ! empty($errors)) {

    // if there are items in our errors array, return those errors
    $data['success'] = false;
    $data['errors']  = $errors;
} else {

    // if there are no errors process our form, then return a message
    // Inserting into the database
    require_once ('insert_user.php');

    // show a message of success and provide a true success variable
    $data['success'] = true;
    $data['message'] = 'Success!';
}
//return all our data on AJAX call
echo json_encode($data);


//creating the test_puts functions
function test_Inputs($data)
{
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
}

您知道AJAX代码中的这一点吗:url:'signUpValidation.php',
既然你的文件是signUp.php

这需要很多代码,但是您调用signUp.php之后需要一次_('insert_user.php');在它中,然后在insert_user中,您再次使用$_POST[“全名”],即使您已经在signUp.php中验证了它。insert_user.php不应该只在数据库中进行查询吗

您正在检查insert_user.php中是否有(isset($_POST['submit'])。您应该在signUp.php中检查它

如果不是类的话,我至少会在函数中组织代码:functionConnectToDB、FInsertUser

signUp.php:

require_once('functions.php');

if(isset($_POST['submit'])) {
    validations you have
}

if (empty($errors)) {
    fill $data array with $full_Name etc. cause you didn't do it!
    insertUser($data);
}
functions.php:

function connectDB() {
    ...
}
function insertUser($data) {
    connectDB();
    insert DB query
    ...
}

在AJAX代码url:'signUpValidation.php'中您知道这一点吗, 既然你的文件是signUp.php

这需要很多代码,但是您调用signUp.php之后需要一次_('insert_user.php');在它中,然后在insert_user中,您再次使用$_POST[“全名”],即使您已经在signUp.php中验证了它。insert_user.php不应该只在数据库中进行查询吗

您正在检查insert_user.php中是否有(isset($_POST['submit'])。您应该在signUp.php中检查它

如果不是类的话,我至少会在函数中组织代码:functionConnectToDB、FInsertUser

signUp.php:

require_once('functions.php');

if(isset($_POST['submit'])) {
    validations you have
}

if (empty($errors)) {
    fill $data array with $full_Name etc. cause you didn't do it!
    insertUser($data);
}
functions.php:

function connectDB() {
    ...
}
function insertUser($data) {
    connectDB();
    insert DB query
    ...
}

可能的值应该用单引号括起来,比如这个值(“$username”,“$password”);在insertUser.phpWARNING中:使用错误抑制
@
操作符可以掩盖代码中的问题,并使调试问题变得更加复杂。这是最后的手段,只能在特殊情况下使用。可能是值应该用单引号括起来,比如这个值(“$username”,“$password”);在insertUser.phpWARNING中:使用错误抑制
@
操作符可以掩盖代码中的问题,并使调试问题变得更加复杂。这是一个万不得已的工具,只能在特殊情况下使用。你能告诉我你用代码片段是什么意思吗
function connectDB() {
    ...
}
function insertUser($data) {
    connectDB();
    insert DB query
    ...
}