簡體   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