gpt4 book ai didi

javascript - JQuery Form仅在某些浏览器中提交

转载 作者:行者123 更新时间:2023-12-02 15:36:11 26 4
gpt4 key购买 nike

我正在为我的网站制作注册表单,它只能在 Firefox 中运行(存在一些问题),但在 Chrome 中根本不起作用。

例如,如果我在 Firefox 中正确填写表单并提交,PHP 页面仍会加载,而不是通过 ajax 调用将该内容发送到我的注册表单。但是在 Chrome 中,表单似乎根本没有提交。

事实上,在 Chrome 中,变量 data 在我的 PHP 脚本中设置为 $num 值,然后显示在 #modal-body.

问题:如何才能使其在所有浏览器中正常工作?为什么我的 event.preventdefault() 在 Firefox 上无法正常工作?
< br/>这是我的注册表提交代码:

$('#register-form').submit(function() {
$('#register-form > .input-group').removeClass('has-error');

$('#register-form .form-required').each(function() {
if ($(this).val().trim() == "") {
empty = true;
$(this).parent().addClass('has-error');
errors = "<strong>You have not filled out the login form correctly.</strong>";
}
else {
$(this).parent().removeClass('has-error');
$(this).parent().addClass('has-success');
}
});

// All fields populated, process the form
if (!empty) {
$('.progress').fadeIn(800);
$('#modal-body').parent().removeClass("has-error");

var formData = {
'username' : $('input[name=usernameReg]').val(),
'email' : $('input[name=email]').val(),
'password' : $('input[name=passwordReg]').val()
};

// Process the form
$.ajax({
type : 'POST',
url : 'db.php',
data : formData,
dataType : 'json',
encode : true,
success : function(data) {

if (data) {

// Fade the resulting message in
setTimeout(function() {
$(".progress").fadeOut(1000);
$("#modal-body").fadeIn(1000);
$("#modal-body").html(data);
}, 1000);

// Fade the resulting message out
setTimeout(function() {
$("#modal-body").fadeOut(2000);
}, 3000);
}

else {
$('#modal-body').addClass('has-error');
$('#modal-body').html(data);
}
}
});
}
// There were empty fields on the form
else {
$('#modal-body').html(errors);
}
event.preventDefault();
});

这是我的 PHP 代码,用于处理表单并检查以确保帐户尚不存在,然后在数据库中创建它:

            $hashed = md5($salt.$_POST['passwordReg']);
$email = $_POST['email'];

// Check if account already exists
$username = strtolower($_POST['usernameReg']);
$statement = $conn->prepare($selects['username']);
$statement->bind_param('ss', $hashed, $username);

if ($statement->execute()) {
//$statement->bind_result($results);
$num;
while($statement->fetch()){
$num++;
}
$statement->free_result();
}
else {
trigger_error('Error executing MySQL query: ' . $statement->error);
}

$statement = null;

// If num > 0, account exists.
// printf($num) only present for debugging purposes
printf($num);
if ($num < 1) {
$statement = $conn->prepare("INSERT INTO `User` (username, email, password) VALUES(?,?,?)");
$statement->bind_param('sss', $username, $email, $hashed);

if ($statement->execute()) {
echo json_encode('You have registered successfully');
}
else {
echo json_encode('Sorry, registration failed. Please try again later.');
}
}
else {
echo json_encode('Sorry, registration failed. Please verify that you do not already have an account.');
}
break;

最佳答案

将事件传递到回调中

$('#register-form').submit(function(e) {
// your code...
e.preventDefault();
}

关于javascript - JQuery Form仅在某些浏览器中提交,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32909086/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com