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

我想在提交按钮验证后提交表单。验证更正并完成后,错误和警告消失,但此脚本不执行任何操作。我单击提交按钮,它只是没有响应,也没有将任何数据插入数据库。


顺便说一下,我在每个单独的 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>


侃侃尔雅
浏览 99回答 1
1回答

沧海一幻觉

值得注意的一件事是您可以优化 AJAX 功能。绝对没有理由发出那么多 AJAX 请求。您可以在一个 AJAX 请求中发送所有数据并完成所有成功功能。另一件需要注意的事情是,如果 post 变量存在,您的 PHP 代码将执行数据库逻辑submit。现在您根本不通过 AJAX 函数解析它。您没有使用带有提交的序列化方法,而是解析非常具体的数据,通过指定每个元素值手动获取。您可以做的就是解析submit为另一个数据变量。我冒昧地根据这个想法优化了您的 AJAX 代码。jQuery AJAX 示例:$(document).ready(function() {&nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; $('#submit').click(function(event) {&nbsp; &nbsp; &nbsp; &nbsp; event.preventDefault();&nbsp; &nbsp; &nbsp; &nbsp; var fullname = $("#fullname").val();&nbsp; &nbsp; &nbsp; &nbsp; var username = $("#username").val();&nbsp; &nbsp; &nbsp; &nbsp; var email = $("#email").val();&nbsp; &nbsp; &nbsp; &nbsp; var password = $("#password").val();&nbsp; &nbsp; &nbsp; &nbsp; var submit = "1";&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; $.ajax({&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; url: 'registercontrol.php',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; method: 'POST',&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; data: {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; fullname : fullname,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; username : username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; email : email,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; password : password,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; submit : submit&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; success:function(response){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#vfullname").html(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#vusername").html(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#vemail").html(response);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; $("#vpassword").html(response);&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; });&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp; &nbsp; });});现在您肯定会有一个提交POST变量,它将输入if()数据库插入的语句。您可以做的另一件事是更具体地检查是否应该输入允许数据库插入的语句。现在它只围绕POST变量submit。没有其他逻辑。你可能想重新考虑一下。创建变量,FALSE当验证检查一切正常时,将它们设置为 true。相反,围绕它构建数据库插入if()语句,因为这比提交变量是否存在更相关。另一件事是您md5()对密码使用哈希函数。这是非常不安全的。参考这篇文章。您也没有在告诉用户单击激活链接的行上正确连接 PHP 变量。你连接super globals得很好,但没有连接 PHP 变量。话虽如此,除了我指出的之外,没有什么本质上的错误。这是你的 PHP 代码:<?phpif( isset( $_POST['fullname'] ) ) {&nbsp; &nbsp; //fullname validation&nbsp; &nbsp; $fullname = $_POST['fullname'];&nbsp; &nbsp; if( empty( $_POST['fullname'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningfn = "Please fill this field";&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #fullname {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningfn.'</p>';&nbsp; &nbsp; } else if( !$user->isValidFullname($fullname) ) {&nbsp; &nbsp; &nbsp; &nbsp; $infofn = 'Your name must be alphabetical characters';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #fullname {border-color: #36b9cc !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-info">'.$infofn.'</p>';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #fullname {border-color: #1cc88a !important;} </style>';&nbsp; &nbsp; }&nbsp; &nbsp;}if( isset( $_POST['username'] ) ) {&nbsp; &nbsp; //username validation&nbsp; &nbsp; $username = $_POST['username'];&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( empty( $_POST['username'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningun = "Please fill this field";&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #username {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningun.'</p>';&nbsp; &nbsp; } else if( !$user->isValidUsername($username) ) {&nbsp; &nbsp; &nbsp; &nbsp; $infoun = 'Your username must be at least 3 alphanumeric characters';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #username {border-color: #36b9cc !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-info">'.$infoun.'</p>';&nbsp; &nbsp; } else if ( !$user->isUsernameAlreadyinUse($username) ) {&nbsp; &nbsp; &nbsp; &nbsp; $errorun = 'This username already in use';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #username {border-color: #e74a3b !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-danger">'.$errorun.'</p>';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #username {border-color: #1cc88a !important;} </style>';&nbsp; &nbsp; }&nbsp; &nbsp;}if( isset( $_POST['email'] ) ) {&nbsp; &nbsp; //email validation&nbsp; &nbsp; $email = htmlspecialchars_decode( $_POST['email'], ENT_QUOTES );&nbsp; &nbsp;&nbsp;&nbsp; &nbsp; if( empty( $_POST['email'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningm = "Please fill this field";&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningm.'</p>';&nbsp; &nbsp; } else if( !$user->isValidEmail($email) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningm = 'Please enter a valid email address';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #email {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningm.'</p>';&nbsp; &nbsp; } else if( !$user->isEmailAlreadyinUse($email) ) {&nbsp; &nbsp; &nbsp; &nbsp; $errorm = 'This email already in use';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #email {border-color: #e74a3b !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-danger">'.$errorm.'</p>';&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #email {border-color: #1cc88a !important;} </style>';&nbsp; &nbsp; }}if( isset( $_POST['password'] ) ) {&nbsp; &nbsp; $password= $_POST['password'];&nbsp; &nbsp; if( empty( $_POST['password'] ) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningpw = "Please fill this field";&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningpw.'</p>';&nbsp; &nbsp; } else if ( !$user->isValidPassword($password) ) {&nbsp; &nbsp; &nbsp; &nbsp; $warningpw = 'Your password must be at least 6 characters long';&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #password {border-color: #f6c23e !important;} </style>';&nbsp; &nbsp; &nbsp; &nbsp; echo '<p class="p-3 text-warning">'.$warningpw.'</p>';&nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; echo '<style type="text/css"> #password {border-color: #1cc88a !important;} </style>';&nbsp; &nbsp; }&nbsp; &nbsp;}if( isset( $_POST['gender'] ) ) {&nbsp; &nbsp; $gender = $_POST['gender'];&nbsp; &nbsp; if( !in_array($gender, ['Male','Female','Other']) ) {&nbsp; &nbsp; &nbsp; &nbsp; $gender = 'Other';&nbsp; &nbsp; }} else {&nbsp; &nbsp; $gender = 'Other';}if( isset( $_POST['submit'] ) ) {&nbsp; &nbsp; //hash the password&nbsp; &nbsp; $hashedpassword = password_hash( $password, PASSWORD_BCRYPT );&nbsp; &nbsp; //create the activasion code&nbsp; &nbsp; // this is highly insecure, see: https://www.php.net/manual/en/function.md5.php&nbsp; &nbsp; $activasion = md5( uniqid( rand(),true ) );&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; //insert into database with a prepared statement&nbsp; &nbsp; &nbsp; &nbsp; $stmt = $db->prepare('INSERT INTO members (fullname,username,password,email,gender,active) VALUES (:fullname, :username, :password, :email, :gender, :active)');&nbsp; &nbsp; &nbsp; &nbsp; $stmt->execute(array(&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':fullname' => $fullname,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':username' => $username,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':password' => $hashedpassword,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':email' => $email,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':gender' => $gender,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ':active' => $activasion&nbsp; &nbsp; &nbsp; &nbsp; ));&nbsp; &nbsp; &nbsp; &nbsp; $id = $db->lastInsertId('memberID');&nbsp; &nbsp; &nbsp; &nbsp; //send email&nbsp; &nbsp; &nbsp; &nbsp; $to = $_POST['email'];&nbsp; &nbsp; &nbsp; &nbsp; $subject = "Confirm Your Account";&nbsp; &nbsp; &nbsp; &nbsp; $body = "<p>Thank you for registering on the demo site.</p>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;<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>";&nbsp; &nbsp; &nbsp; &nbsp; $mail = new Mail();&nbsp; &nbsp; &nbsp; &nbsp; $mail->setFrom(SITEEMAIL);&nbsp; &nbsp; &nbsp; &nbsp; $mail->addAddress($to);&nbsp; &nbsp; &nbsp; &nbsp; $mail->subject($subject);&nbsp; &nbsp; &nbsp; &nbsp; $mail->body($body);&nbsp; &nbsp; &nbsp; &nbsp; $mail->send();&nbsp; &nbsp; &nbsp; &nbsp; //redirect to index page&nbsp; &nbsp; &nbsp; &nbsp; header('Location: register.php?action=joined');&nbsp; &nbsp; &nbsp; &nbsp; exit;&nbsp; &nbsp; &nbsp; //else catch the exception and show the error.&nbsp; &nbsp; } catch(PDOException $e) {&nbsp; &nbsp; &nbsp; &nbsp; $error[] = $e->getMessage();&nbsp; &nbsp; }}?>我相信这$someVar->isValid()指的是有效的东西,因为我对此没有其他见解。如果您现在在数据库插入之外遇到更多错误,则问题出在其他地方。要么你没有遵循你的表结构逻辑(拼写错误、无效的数据格式等)
打开App,查看更多内容
随时随地看视频慕课网APP