0

I am trying to make a simple form that when certain text is entered it will redirect to another page.

Form:

<form action="goto.php" method="post">
Destination: <input type="text" name="destination">
<input type="submit">
</form>

I am unsure of how to set up goto.php to achieve the desired result. I basically want something like the following:

<?php 
if ($_POST["destination"]="mail" ) {
    header( 'Location: /mail/' );
} elseif ($_POST["destination"]="forms") {
    header( 'Location: /forms/' );
} else {
    echo "Invalid";
}
?>

However, this doesn't work because the way header(); works makes the form go to /mail/ no matter what text is entered. How can I resolve this to achieve my desired result?

Christopher
  • 2,103
  • 4
  • 22
  • 32

2 Answers2

1

you could do something like that. In your condition you just attribute the destination and you make only one header(...)

<?php 
if ($_POST["destination"] === "mail" ) {
    $destination = '/mail/';
} elseif ($_POST["destination"] === "forms") {
    $destination = '/forms/';
} else {
    echo "Invalid";
    return;
}
header( 'Location: ' . $destination );
?>
olibiaz
  • 2,551
  • 4
  • 29
  • 31
1

You're assigning 'mail' to $_POST["destination"] which returns true so the if is valid

Do this instead:

<?php 
if ($_POST["destination"] =="mail" ) {//Note the ==
    header( 'Location: /mail/' );
} elseif ($_POST["destination"]=="forms") { //Note the ==
    header( 'Location: /forms/' );
} else {
    echo "Invalid";
}
?>

See this for more on Comparison Operators

Hope this helps!

Mike Donkers
  • 3,589
  • 2
  • 21
  • 34