gpt4 book ai didi

javascript - 数据未从 javascript 函数发送到 php 表单

转载 作者:行者123 更新时间:2023-11-28 18:52:22 24 4
gpt4 key购买 nike

我正在尝试使用 JavaScript 和 PHP 制作注册表单,但遇到问题。 JavaScript 代码不会将数组发送到 register_submit.php 。我尝试查看互联网上的一些示例以及 StackOverflow 上的其他帖子,但似乎没有一个答案可以解决我自己的问题。

用户来到registration.php页面,填写表单并点击提交按钮,该按钮调用checkForms()方法。 setValues() 方法将注册表中的值设置为各个变量。我已经检查了检索到的变量是否正确(它们是正确的)。

编辑:问题不在于有两个 $password 变量。意外测试错误。

JavaScript 代码:

function checkForms() {
setValues();
callPHP();
}

function callPHP() {


alert(registration.emailR);
// call ajax
$.ajax({
type: "POST",
url: 'register_submit.php',
data:{name:registration.usernameR, email:registration.emailR, password:registration.password1R, forename:registration.forenameR, surname:registration.surnameR, country:registration.countryR, dob:registration.dobR},
success:function(msg) {
alert("Register Complete");
}
});

// Go to homepage
window.location.href = "index.php";

}

PHP 代码:

<?php
$username = $_POST['username'];
$email = $_POST['email'];
$password = $_POST['password'];
$forename = $_POST['forename'];
$surname = $_POST['surname'];
$country = $_POST['country'];
$dob = $_POST['dob'];

$host = "host";
$user = "user";
$password = "password";
$database = "database";

$con = new mysqli($host, $user, $password, $database);

if(!$con)
die('Connection failed'.mysql_error());
else echo "Connection successful ";
mysqli_select_db($con, $database);

$query = "INSERT INTO user (userID, username, dob, email, password, isAuthor, isAdmin, country, surname, firstname) VALUES ('user001', '$username', '$dob', '$email', '$password', '0', '0', '$country', '$surname', '$forename')";
echo $query;
mysqli_query($con, $query);
echo "Registration Complete!!";
?>

最佳答案

Ajax 是异步的,这意味着它在后台工作,直到工作完成,您将在 ajax 完成之前使用此行导航离开页面:

window.location.href = "index.php";

将其放入成功方法中:

success:function(msg)
{
alert ("Register Complete");
window.location.href = "index.php";
}

关于javascript - 数据未从 javascript 函数发送到 php 表单,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34291978/

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