0

I have the following code:

if($_POST['profileName']!="") {
    $profileName = $_POST['profileName'];

    if (!($stmt = $con->prepare("UPDATE `user_settings` SET ProfileName=?,ProfileSetup=? WHERE Username=?")) || !is_object($stmt)) {
        die( "Error preparing: (" .$con->errno . ") " . $con->error);
    }
    $stmt->bind_param('sis', $profileName, 1, $_SESSION['user']);
    if($stmt->execute()) { 
        echo '<div class="alert_successful alert_absolute_center">';
        echo 'Profile Name change Successful!';
        echo '</div>';
    } else {
        echo '<div class="alert_unsuccessful alert_absolute_center">';
        echo 'Something went wrong... Try again!';
        echo '</div>';
    }
}

I would like to be able to redirect to different places depending on the result of the if statement... I have tried setting variables and then using a meta redirect, but of course that didn't work because it was looking for the variable before it was actually set...

Any help is much appreciated.

1
  • echo '<script>location="/test"</script>'; Commented Mar 12, 2014 at 3:16

2 Answers 2

2

If you are using Output buffering you may be able to set a header redirect:

if($stmt->execute())
    header('Location: your_page_success.php');
else
    header('Location: your_page_fail.php');

Otherwise the header call wont work if you have already started outputting data (e.g. with echo).

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

8 Comments

How would this help in my circumstance? The redirect has to go within the if statement?
As per your comment line, this answer is no appropriate for the OP's question since he is echoing HTML.
I'd make a habit of adding exit() after as well.
Yep, add exit; after the header() to prevent executing the rest of the script.
On its own it is not. It does not work with the OP's current code at all and makes no effort to suggest other solutions for the response messages.
|
0

Redirecting is easy. It's your approach that needs some work.

Essentially, here's what you want to do:

  1. Go somewhere (your current script)
  2. Figure out what you want to do with what you have (your current logic)
  3. Show the user some feedback about what you did is step #2
    Which may or may not be redirecting the user to a different location

First of all, I would suggest redirecting the user, even if it's to the same page. This will prevent the, "Would you like to submit this form again?" type alerts the user would get if they were to refresh the page after they had submitted a form.

The best way to redirect is using PHP's header function. Just make sure that nothing is printed to the page before the you execute this command. Also, always follow it by an exit();.

Now what about the user's feedback message? There are a number of ways you can handle this; here's a couple:

  1. Have a predefined and indexed list of messages (whether it's hard-coded, in a database, or in a config file somewhere. When you redirect, add the index in the URL as a parameter, something like http://redirecttohere.com/?msg=badpwd. You can now take this parameter and retrieve and display the appropriate message.
  2. *PREFERED* Store your message in a session variable, redirect, retrieve message from session, display message, destroy said session variable. Check out this guy's solution; use it and/or check out his source code and figure out how he did it.

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.