繁体   English   中英

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

[英]Data not being sent to a php form from a javascript function

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

用户进入registration.php页面,填写表单并点击Submit按钮,该按钮调用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"; 
} 

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM