为了账号安全,请及时绑定邮箱和手机立即绑定

Ajax 提交验证成功响应后不执行任何操作

Ajax 提交验证成功响应后不执行任何操作

PHP
侃侃尔雅 2023-07-01 09:40:56
我想在提交按钮验证后提交表单。验证更正并完成后,错误和警告消失,但此脚本不执行任何操作。我单击提交按钮,它只是没有响应,也没有将任何数据插入数据库。顺便说一下,我在每个单独的 div 元素中显示了每个输入验证,所以我也不想破坏这种风格。<script type="text/javascript">    $(document).ready(function(){               $('#submit').click(function(event){            event.preventDefault();                     var fullname = $("#fullname").val();            var username = $("#username").val();            var email = $("#email").val();            var password = $("#password").val();                        $.ajax({                url: 'registercontrol.php',                method: 'POST',                data: {fullname:fullname},                success:function(response){                                     $("#vfullname").html(response);                                 }            });            $.ajax({                url: 'registercontrol.php',                method: 'POST',                data: {username:username},                success:function(response){                                     $("#vusername").html(response);                                 }            });            $.ajax({                url: 'registercontrol.php',                method: 'POST',                data: {email:email},                success:function(response){                                     $("#vemail").html(response);                                    }            });            $.ajax({                url: 'registercontrol.php',                method: 'POST',                data: {password:password},                success:function(response){                                     $("#vpassword").html(response);                                 }            });                             });    });</script>
查看完整描述

1 回答

?
沧海一幻觉

TA贡献1824条经验 获得超5个赞

值得注意的一件事是您可以优化 AJAX 功能。绝对没有理由发出那么多 AJAX 请求。您可以在一个 AJAX 请求中发送所有数据并完成所有成功功能。


另一件需要注意的事情是,如果 post 变量存在,您的 PHP 代码将执行数据库逻辑submit。现在您根本不通过 AJAX 函数解析它。您没有使用带有提交的序列化方法,而是解析非常具体的数据,通过指定每个元素值手动获取。


您可以做的就是解析submit为另一个数据变量。我冒昧地根据这个想法优化了您的 AJAX 代码。


jQuery AJAX 示例:


$(document).ready(function() {       

    $('#submit').click(function(event) {

        event.preventDefault();

        var fullname = $("#fullname").val();

        var username = $("#username").val();

        var email = $("#email").val();

        var password = $("#password").val();

        var submit = "1";

        

        $.ajax({

            url: 'registercontrol.php',

            method: 'POST',

            data: {

                fullname : fullname,

                username : username,

                email : email,

                password : password,

                submit : submit

            },

            success:function(response){                 

                $("#vfullname").html(response);

                $("#vusername").html(response);

                $("#vemail").html(response);

                $("#vpassword").html(response);  

            }

        });                     

    });

});

现在您肯定会有一个提交POST变量,它将输入if()数据库插入的语句。


您可以做的另一件事是更具体地检查是否应该输入允许数据库插入的语句。现在它只围绕POST变量submit。没有其他逻辑。你可能想重新考虑一下。创建变量,FALSE当验证检查一切正常时,将它们设置为 true。相反,围绕它构建数据库插入if()语句,因为这比提交变量是否存在更相关。


另一件事是您md5()对密码使用哈希函数。这是非常不安全的。参考这篇文章。


您也没有在告诉用户单击激活链接的行上正确连接 PHP 变量。你连接super globals得很好,但没有连接 PHP 变量。


话虽如此,除了我指出的之外,没有什么本质上的错误。


这是你的 PHP 代码:


<?php

if( isset( $_POST['fullname'] ) ) {

    //fullname validation

    $fullname = $_POST['fullname'];


    if( empty( $_POST['fullname'] ) ) {

        $warningfn = "Please fill this field";

        echo '<style type="text/css"> #fullname {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningfn.'</p>';

    } else if( !$user->isValidFullname($fullname) ) {

        $infofn = 'Your name must be alphabetical characters';

        echo '<style type="text/css"> #fullname {border-color: #36b9cc !important;} </style>';

        echo '<p class="p-3 text-info">'.$infofn.'</p>';

    } else {

        echo '<style type="text/css"> #fullname {border-color: #1cc88a !important;} </style>';

    }   

}


if( isset( $_POST['username'] ) ) {

    //username validation

    $username = $_POST['username'];

    

    if( empty( $_POST['username'] ) ) {

        $warningun = "Please fill this field";

        echo '<style type="text/css"> #username {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningun.'</p>';

    } else if( !$user->isValidUsername($username) ) {

        $infoun = 'Your username must be at least 3 alphanumeric characters';

        echo '<style type="text/css"> #username {border-color: #36b9cc !important;} </style>';

        echo '<p class="p-3 text-info">'.$infoun.'</p>';

    } else if ( !$user->isUsernameAlreadyinUse($username) ) {

        $errorun = 'This username already in use';

        echo '<style type="text/css"> #username {border-color: #e74a3b !important;} </style>';

        echo '<p class="p-3 text-danger">'.$errorun.'</p>';

    } else {

        echo '<style type="text/css"> #username {border-color: #1cc88a !important;} </style>';

    }   

}


if( isset( $_POST['email'] ) ) {

    //email validation

    $email = htmlspecialchars_decode( $_POST['email'], ENT_QUOTES );

    

    if( empty( $_POST['email'] ) ) {

        $warningm = "Please fill this field";

        echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningm.'</p>';

    } else if( !$user->isValidEmail($email) ) {

        $warningm = 'Please enter a valid email address';

        echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningm.'</p>';

    } else if( !$user->isEmailAlreadyinUse($email) ) {

        $errorm = 'This email already in use';

        echo '<style type="text/css"> #email {border-color: #e74a3b !important;} </style>';

        echo '<p class="p-3 text-danger">'.$errorm.'</p>';

    } else {

        echo '<style type="text/css"> #email {border-color: #1cc88a !important;} </style>';

    }

}


if( isset( $_POST['password'] ) ) {

    $password= $_POST['password'];


    if( empty( $_POST['password'] ) ) {

        $warningpw = "Please fill this field";

        echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningpw.'</p>';

    } else if ( !$user->isValidPassword($password) ) {

        $warningpw = 'Your password must be at least 6 characters long';

        echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';

        echo '<p class="p-3 text-warning">'.$warningpw.'</p>';      

    } else {

        echo '<style type="text/css"> #password {border-color: #1cc88a !important;} </style>';

    }   

}


if( isset( $_POST['gender'] ) ) {

    $gender = $_POST['gender'];

    if( !in_array($gender, ['Male','Female','Other']) ) {

        $gender = 'Other';

    }

} else {

    $gender = 'Other';

}


if( isset( $_POST['submit'] ) ) {

    //hash the password

    $hashedpassword = password_hash( $password, PASSWORD_BCRYPT );


    //create the activasion code

    // this is highly insecure, see: https://www.php.net/manual/en/function.md5.php

    $activasion = md5( uniqid( rand(),true ) );


    try {

        //insert into database with a prepared statement

        $stmt = $db->prepare('INSERT INTO members (fullname,username,password,email,gender,active) VALUES (:fullname, :username, :password, :email, :gender, :active)');

        $stmt->execute(array(

            ':fullname' => $fullname,

            ':username' => $username,

            ':password' => $hashedpassword,

            ':email' => $email,

            ':gender' => $gender,

            ':active' => $activasion

        ));

        $id = $db->lastInsertId('memberID');


        //send email

        $to = $_POST['email'];

        $subject = "Confirm Your Account";

        $body = "<p>Thank you for registering on the demo site.</p>

                 <p>Hello ".$fullname.", please click this link to activate your account: <a href='".DIR."activate.php?x=".$id."&y=".$activasion."'>".DIR."activate.php?x=".$id."&y=".$activasion."</a></p>";


        $mail = new Mail();

        $mail->setFrom(SITEEMAIL);

        $mail->addAddress($to);

        $mail->subject($subject);

        $mail->body($body);

        $mail->send();


        //redirect to index page

        header('Location: register.php?action=joined');

        exit;


      //else catch the exception and show the error.

    } catch(PDOException $e) {

        $error[] = $e->getMessage();

    }

}

?>

我相信这$someVar->isValid()指的是有效的东西,因为我对此没有其他见解。


如果您现在在数据库插入之外遇到更多错误,则问题出在其他地方。要么你没有遵循你的表结构逻辑(拼写错误、无效的数据格式等)


查看完整回答
反对 回复 2023-07-01
  • 1 回答
  • 0 关注
  • 97 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信