0

我正在尝试设置一个注册框来创建新帐户。我正在尝试通过 ajax 加载 html 表单并将数据传递给 php 文件。

我想让包含表单的 div 每次点击“注册”按钮以从 php 脚本中获取结果并将其显示出来时重新加载。但是,我的代码似乎无法正常工作(处理 ajax 的 div 不会加载表单)。以下是我的代码:

注册.php:

<?php

            session_start();
            $email = $_POST['email'];
            $email = mysql_real_escape_string($email);
            $pwd = $_POST['pwd'];
            $repwd = $_POST['repwd'];
            $lname = $_POST['lname'];
            $fname = $_POST['fname'];

            $isValidEmail = 1;
            if (substr_count($email, '@') != 1){
                    $isValidEmail = 0;
            }

            if($pwd != $repwd){ //check if password and re-entered passwords are the same

                $_SESSION['error']      = 1;
                $_SESSION['message']    = 'Password and Re-entered Password are different.';


            } else if( strlen($pwd) < 6 || strlen($pwd) > 64 ) { //check if password is 6 - 64 characters
                $_SESSION['error']    = 1;
                $_SESSION['message']  = 'Password must be 6 - 64 characters.';

            } else if( strlen($email) > 255) { //check if the email is too long

                $_SESSION['error']      = 1;
                $_SESSION['message']    = 'Email exceeded maximum length.';

            } else if ($isValidEmail != 1){

                $_SESSION['error']     = 1;
                $_SESSION['message']   = 'Invalid Email.';


            } else if (ctype_space($lname) || ctype_space($fname)){
               $_SESSION['error']     = 1;
               $_SESSION['message']   = 'Please enter your name.';

            } else {    
                if ($mysqli = new mysqli("localhost", "root", "", "my_db")){

                    $stmt = $mysqli->prepare("SELECT email FROM users WHERE email = ?");
                    $stmt->bind_param('s',$email);
                    $stmt->execute();
                    $stmt->bind_result($result);
                    $stmt->fetch();
                    $stmt->close();

                    if ($result == $email) { //check if the input email exists in the database, duplicated user
                        $_SESSION['error']    = 1;
                        $_SESSION['message']  = 'Email '.$email.' is already used.';

                    } else {  

                        $hash = hash('sha256', $pwd);

                        function createSalt()
                        {
                            $string = md5(uniqid(rand(), true));
                            return substr($string, 0, 3);
                        }

                        $salt = createSalt();
                        $hash = hash('sha256', $salt . $hash);

                        $stmt = $mysqli->prepare("INSERT INTO users ( email, lastName, firstName, password, salt )
                                VALUES ( ? , ?, ?, ? ,? )");
                        $stmt->bind_param('sssss', $email, $lname, $fname, $hash, $salt);
                        if ($stmt->execute()){
                            $_SESSION['message']  = 'Registered.';

                        } else { 
                            $_SESSION['error']   = 1;
                            $_SESSION['message'] = 'Database query error occured.';

                        }
                        $stmt->close();

                    }
                } else {
                    $_SESSION['error']   = 1;
                    $_SESSION['message'] = 'Error connecting to the database.';

                }
            }
            header("Location: Home.php");   
            $mysqli->close();
        ?>

ajax.js:

$(document).ready(function() {  

                $('#submit_register').click(function(){
                    $('#register_form').submit( function(){
                        $.ajax({
                            type:       'POST',
                            url :       'Register.php',
                            data:       $('#register_form').serialize(),
                            success:    function () {
                                            var myURL = "Register_form.php#register_div";
                                            $('#ajaxHandle').load(myURL);
                                            return false; 
                                        },
                        });
                    });
                });
});

注册表格.php:

<!DOCTYPE html>
<html lang="en">
    <head>
        <?php session_start(); ?>
        <meta charset="utf-8" />
        <title></title>
    </head>
    <body>
        <div class="span-23 prepand-top last" id="register_div" style="background:gray;">
            <div id="wrapper_register" class="span-21 last" style="padding-top: 20px; padding-left:20px; padding-bottom:20px;">
                <form id="register_form" action="register.php" method="post">

                        <legend class="large">Register</legend>
                                <?php

                                    if ($_SESSION['message']){
                                            $class = "";
                                            if ($_SESSION['error']){
                                               $class = "error";
                                            } else {
                                               $class = "success";
                                            }
                                            echo "<div class=\"$class span-4 last\">";
                                            echo $_SESSION['message'];
                                            echo "</div>";

                                            unset ($_SESSION['error']);
                                            unset ($_SESSION['message']); 
                                    }
                                ?>
                        <div class="span-23 prepand-top last">
                        <p>E-mail address: <br>
                        <input type="text" name="email" maxlength="255" /></p><br>
                        <p>Last Name: <br><input type="text" name="lname" maxlength="255" /></p><br>
                        <p>First Name: <br>
                        <input type="text" name="fname" maxlength="255" /></p><br>
                        <p>Password: <br>
                        <input type="password" name="pwd" /><p class="quiet">6 - 64 characters</p><br>
                        <p>Re-enter Password: <br><input type="password" name="repwd" /></p><br>
                        <input id="submit_register" type="submit" value="Register" /><br>
                        </div>

                </form>
            </div>
        </div>
    </body>
</html>

我做错了什么?任何建议将被认真考虑。非常感谢!

4

1 回答 1

0

我想我想通了。我把刷新的 jquery 代码放在了错误的地方。当我把它放在 .submit 范围内时它起作用了:

    $(document).ready(function() {   
        $('#submit_register').click(function(){
            $('#register_form').submit( function(){
                $.post(
                    'Register.php', 
                    $(this).serialize()
                );
                var myURL = "Register_form.php#register_div";
                $('#ajaxHandle').load(myURL);
                return false;
            });
        });
    });
于 2012-06-22T10:29:49.350 回答