0

I'm trying to make you redirect to the successful page after succesfully registering but whenever I click the register button I get redirected instantly even if all the input's are empty

if(isset($_POST["registration"])){
    if(!empty($_POST["gender"])
    and !empty($_POST["username"])
    and !strlen($_POST["username"]) < 3
    and !empty($_POST["email"])
    and !email($_POST["email"])
    and !empty($_POST["emailver"])
    and !verification($_POST["email"],$_POST["emailver"])
    and !empty($_POST["password"])
    and !strlen($_POST["password"]) < 8
    and strlen($_POST["password"]) < 25
    and checkUppercase($_POST["password"])
    and checkLowercase($_POST["password"])
    and checkNumber($_POST["password"])
    and !empty($_POST["passwordver"])
    and !verification($_POST["password"],$_POST["passwordver"])){
        if(addgamer($db,$_SESSION["gender"],$_SESSION["username"],$_SESSION["email"],$_POST["password"]) === TRUE){
            $url = 'succesfull.html';
            header($url);
        }
    }
}

The function for addgamers

function addgamer($db,$gender,$username,$email,$password){

    $sql = "INSERT INTO gamers (`gender`, `username`, `email`, `password`) VALUES('$gender','$username','$email','$password')";
    $db -> exec($sql);
    if ($db->query($sql) === TRUE) {
        $GamerId = $db -> lastInsertId();
        $_SESSION['GamerId'] = $GamerId;
        return true;
    } else {
        return false;
    }
}

I found some answers here but all of them do the same and I have no solution for this.

GOTO($url);

Or

header('location: $url');

Or

if(addgamer($db,$_SESSION["gender"],$_SESSION["username"],$_SESSION["email"],$_SESSION["password"]) === FALSE ){
    die();
}else{
    $url = 'successful.php';
    header($url);
}

Also tried with jQuery

echo "<script>window.location = '$url'</script>";

On every single click on the registration submit button you get redirected to the successful page but that should not happen until everything is filled in correctly as shown from the above code.

As you can also see I'd like to fix this problem trying with php only first.

6
  • 1
    What does addgamer() do? Also, if you're using header() for redirecting, it's used like header("Location: path.html");. Commented Sep 14, 2015 at 16:16
  • Are you sure you aren't unintentionally redirecting from somewhere else? Delete your redirect and just add a print statement to make sure Commented Sep 14, 2015 at 16:16
  • 1
    First couple of points header() should look like this header('Location: xxx.php); and should be followed by an exit; to stop execution at that point. Commented Sep 14, 2015 at 16:18
  • 1
    Please show your <form tag and tell us the name of this form you have shown us. My gues is you are completely confused, as are we Commented Sep 14, 2015 at 16:20
  • 1
    The beginning for my form is: '<form name="registration" action="successful.php" method="post">' Commented Sep 14, 2015 at 16:25

2 Answers 2

2

Validate before submitting the form would be better at first. As @Nicolas D mentioned you should also know that the form action is the page you direct to no matter what.

You can however set this to the current page by using action="" or action="<?php echo $_SERVER['PHP_SELF']; ?>"

This last could come with exploits, you can use "htmlentities($_SERVER['PHP_SELF'])" instead. Read about this here.

Sign up to request clarification or add additional context in comments.

6 Comments

I have tried to use the 'action="<?php echo $_SERVER['PHP_SELF']; ?>"' and seems to work.
@Wanjia , don't forget to rewrite your header(); which should be used like: header('Location: xxx.php'); and BEFORE any HTML.
As this seems to help the problem I had it created a new one. It might be me looking over the badly coded problem but it adds the gamer but does the else statement instead of redirecting.
@Wanjia look at my solution then, it won't be the easiest but it might be the best thing you can do and learn to improve your coding skills :)
I will but when I chanced "$db->query($sql) === TRUE" to "$db->query($sql)" it all seems to work and I don't know how.
|
2

You have a missunderstanding PHP problem. If your form use a submit form, it will post automatically to the php page targeted. If you don't want to post to a new page and still verify your data, then you must use ajax in jquery : http://api.jquery.com/jquery.post/ . With that you ll catch your data without unexpected redirection. Once all your form is validated by your php code during ajax call, you can redirect in jquery on the callback function.

Hope this will help, I ve been through this step and I remember it was painful

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.