【发布时间】:2020-10-16 18:55:50
【问题描述】:
我想在提交按钮验证后提交表单。验证更正并完成后,错误和警告消失,但此脚本什么也不做。我点击了提交按钮,它只是没有响应,也没有将任何数据插入数据库。
顺便说一下,我在每个单独的 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>
registercontrol.php
<?php
require('../includes/config.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>';
}
elseif (! $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>';
}
elseif (! $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>';
}
elseif (! $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>';
}
elseif (! $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>';
}
elseif (! $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>';
}
elseif (! $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 form has been submitted process it
if(isset($_POST['submit'])){
//hash the password
$hashedpassword = password_hash($_POST['password'], PASSWORD_BCRYPT);
//create the activasion code
$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();
}
}
?>
【问题讨论】:
-
您执行这么多 ajax 请求的任何具体原因 - 您是否知道可以在一个请求中发送所有这些数据。如果您需要帮助,请告诉我
-
能否请您也发布您的 PHP 代码?谢谢
-
AJAX 代码可以优化,但本身并没有错。问题出在你的 registercontrol.php
-
@AlwaysHelping 我现在发布了。
-
@Martin 我不这么认为,但我现在发布了也许你可以检查一下。
标签: javascript php jquery ajax pdo