简体   繁体   中英

how to redirect the user to profile page after signing up using ajax call

Hello guys so basically the ajax code below checks for the username and email and makes sure that the user is repetitive and successfully inserts a record into the database. But the problem I am having is the part that says if data == true. I can't get the user to be redirected to their profile page. I mean after a user signs up, the signup form disappears and I am back to the index.php page however, it should redirect them to their profile page and signup and login buttons should disappear from my header.

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");
       }
    });
});

You're mixing PHP and JavaScript here:

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

You need to use JavaScript:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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