Corentin Branquet
Corentin Branquet

Reputation: 1576

Redirect to another page after if condition

After submitting data from a form, I want to be redirected to a page, here's my code :

<form action="#result" method="POST">
  <input name="zipcode" type="text" placeholder="Your ZipCode" />
  <input name="zipcode_submit" type="submit" value="Send" />
</form>

<div id="result">

<?php 

    if(isset($_POST['zipcode_submit'])) {
        header("Location: http://twitter.com");
    }

?>

</div>

It does not work for me and I don't know why

Thanks for your help

Upvotes: 1

Views: 165

Answers (2)

user4628565
user4628565

Reputation:

try to shift the php code, above the form tag,i.e

 <?php 

    if(isset($_POST['zipcode_submit'])) {
        header("Location: http://twitter.com");
    }

   ?>

above

<form action="#result" method="POST">

Upvotes: 1

Marcus Rommel
Marcus Rommel

Reputation: 1294

Have a look at php.net docs regarding the header function. It has to be the first output to the website. Put your form at the end of the file.

  <html>
<?php
    /* this will produce an error, header must be the first output on the website */
    header('Location: http://www.example.com/');
    exit;
    ?>

You should place a exit; after the header statement, to fully prevent the following code from being executed.

Upvotes: 0

Related Questions