繁体   English   中英

JQuery 和 AJAX 表单验证不起作用

[英]JQuery and AJAX form validation not working

我对 Javascript 和 PHP 还很陌生,但需要对用户注册进行表单验证,而且我在将新用户插入数据库时​​遇到了问题。 我通过 JQuery 的验证插件在客户端验证表单,并尝试将请求发送到我通过 AJAX 检查用户可用性的数据库。 验证插件可以工作,但是当我通过 AJAX 请求数据库条目时出现问题,因为如果我按下表单中的“发送”按钮,我会在屏幕上收到Error occurred警报。

我的 registration.html 代码:

<form id="form" name="registration" action="" method="POST">
    <label for="user_name">Nickname:</label><br>
    <input type="input" size="40" maxlength="10" name="user_name" id="user_name" placeholder="Nickname"><br><br>
    
    <label for="user_email">Email:</label><br>
    <input type="email" size="40" maxlength="20" name="user_email" id="user_email" placeholder="Email address"><br><br>
    <label for="user_password">Password:</label><br>
    <input type="password" size="40" maxlength="50" name="user_password" id="user_password" placeholder="Passwort"><br>

    <label for="confirmed_password">Confirm password:</label><br>
    <input type="password" size="40" maxlength="50" name="confirmed_password" id="confirmed_password" placeholder="Passwort bestätigen"><br><br>

    <input type="submit" name="register" value="Send">
</form>
<script src="./form_validation.js"></script>

我的 form_validation.js 代码:

    $("#form[name='registration']").validate({
        rules: {
            user_name: {
                required: true
            },
            user_email: {
                required: true,
                email: true
            },
            user_password: {
                required: true,
                minlength: 7
            },
            confirmed_password: {
                required: true,
                equalTo: "#user_password"
            }
        },
        messages: {
            user_name: 'Enter a nickname.',
            user_email: {
                required: 'Enter your email address.',
                email: 'Enter a valid email address.',
            },
            user_password: {
                required: 'Enter a password.',
                minLength: 'At least 7 characters.',
            },
            confirmed_password: {
                required: 'Confirm password.',
                equalTo: 'Passwords have to match.',
            }
        },
        submitHandler: function (form) { 
            $.ajax({
                url:'./user.php',
                type: "post",
                data: $(form).serialize(),
                success: function() {
                    alert("success");
                    console.log();
                    $("#result").html('Submitted');
                },
                error: function() {
                    alert("Error occured");
                    console.log();
                    $("#result").html('An error occured while submitting.');
                }       
            });
        }
    });
});

我的 user.php 代码:

<?php

require_once __DIR__ . "./connection.php";

if (isset($_POST["register"]) && isset($_POST['user_name']) && isset($_POST['user_email']) && isset($_POST['user_password']) && isset($_POST['confirmed_password'])) 
{
    $user_name = $_POST['user_name'];
    $user_email = $_POST['user_email'];
    $user_password = $_POST['user_pasword'];

    $db = new DB_connection;

    $sql = "SELECT * FROM user WHERE user_name = :user_name OR user_email = :user_email";

    $statement = $db->conn->prepare($sql);
    $statement->bindParam("user_name", $user_name);
    $statement->bindParam("user_email", $user_email);
    
    if ($statement->execute())
    {
        $sql =  "INSERT INTO user (user_name, user_pass, user_email) VALUES (:user_name, :user_password, :user_email)"; 

        $stmt = $db->conn->prepare($sql);
        $stmt->bindParam(":user_name", $user_name);
        $stmt->bindParam(":user_password", $user_password);
        $stmt->bindParam(":user_email", $user_email);

        if ($stmt->execute()) {
            echo "New user inserted";
        } else {
            echo "Something went wrong!";
        }
    } else {
        echo "Error!";
    }
}

找到了解决办法。 我只需要替换include_once __DIR__ './connection.php'; 使用include_once __DIR__ '/../config/config.php'; . 奇怪的是没有出现 MYSQL 错误消息,其他类在没有include_once语句的情况下工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM