0

HI ,

Can any one help

I need to direct the user once they have submitted the form to another page

How could I do this ?

<form enctype="multipart/form-data" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">

3 Answers 3

1

If you want the POST + Redirect thing:

header('Location: ' . $nextUrl, true, 303);

will do the trick.

Example code:

<!-- form.html -->
<form method="post" action="postHandler.php">
  <input type="text" name="someName" value=""/>
  <input type="submit" name="submitButton">
</form>

 

// postHandler.php
if (isset($_POST['submitButton'])) {
  // do something with the posted data

  header('Location: submitOk.html', true, 303);
} else {
  header('Location: form.html', true, 303);
}

 

<!-- submitOk.html -->
<h1>Ok</h1>
<p>
  Your information was received
</p>
Sign up to request clarification or add additional context in comments.

2 Comments

Could you just explain how it works , So I learn something :)
@Oliver he explained it, you need 3 pages: form.html, postHandler.php (handles the form post), and 3rd page to redirect to submitOk.html. The form page submits to the postHandler, the post handler does something with the form data, and then redirects user to the 3rd page that shows they submitted ok.
0

Specify the page in the action="" attribute of the form (don't use PHP_SELF).

1 Comment

this won't help the user, it will just submit the form to another page other than SELF.
0

In the script when a successful POST has finished:

header('Location: http://'.$_SERVER['HTTP_HOST'].'/finalpage.php', TRUE, 303);

Note this should include an absolute URL, not just /finalpage.php; relative URIs in a Location header are not allowed by the HTTP RFC and not supported by some browsers. Also 303 is strictly speaking more correct than the default 302.

If you want the redirect to simply fetch the same page again as a GET you can do:

header('Location: '.$_SERVER['PHP_SELF'], TRUE, 303);

Also:

action="<?php echo $_SERVER['PHP_SELF']; ?>"

is insecure, potentially a HTML-injection hole leading to XSS. All text strings being output into HTML content or attribute values must be escaped appropriately:

action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>"

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.