使用ajax调用注册后如何将用户重定向到个人资料页面

时间:2016-06-16 18:53:35

标签: php jquery ajax

大家好,所以基本上下面的ajax代码会检查用户名和电子邮件,并确保用户重复并成功将记录插入数据库。但我遇到的问题是如果data == true则说的部分。我无法将用户重定向到他们的个人资料页面。我的意思是在用户注册后,注册表单消失,我回到index.php页面,但是,它应该将它们重定向到他们的个人资料页面,注册和登录按钮应该从我的标题中消失。

var signupUNameBad = true;
var signupEmailBad = true;

// sign up button selected
$("#signup").submit(function(e) { 
$("#signup").css('visibility','hidden')
e.preventDefault();
if (signupUNameBad == true){
    alert("The User Name Selected is already in use, Please choose a different User Name");
    return;
}
if (signupEmailBad == true) {
    alert("The Email is already in use on the system, Please choose a different email")
    return;
}
else
// add person to DB
$.ajax({
    url: 'enterUserBasic.php',
    type: 'post',
    dataType: 'text',
    async: 'false',
    data: "fname=" + $("#Sfname").val() + "&lname="+ $("#Slname").val() + "&pass=" + $("#Spass").val() + "&uName=" + $("#signupUserName").val() + "&email=" + $("#signupEmail").val(),  
    success: function (data) { 
        // if data = true then the user was added, false the user was declined for some reason
        if (data == "true") {
            header('location: profile.php');
        }
        else if (data == "false"){
            alert("database error, please try again");
        }
    },
       error: function (data) {
           alert("Some kind of error occured in login, please try again");
       }
    });
});

1 个答案:

答案 0 :(得分:2)

你在这里混合使用PHP和JavaScript:

 if (data == "true") {
     header('location: profile.php'); // PHP code will not work here
 }

您需要使用JavaScript:

 if (data == "true") {
    window.location = "profile.php";
 }