0

How do I make the statement below work. I have a form that post the values to my function.php. Is it possible for me to include the .html files on the function.php as a content rather than heading to the locations? I am new student learning the hard way.

<?php
    if ($_POST['city'] = 'london'){
     header('Location: london.html');
    }
    if ($_POST['city'] = 'manchester'){
    header('Location: manchester.html');
    }
    if ($_POST['city'] = 'conventry'){
    header('Location: coventry.php');
    }
    exit;

?>

2 Answers2

0

You have to use == in if statement, not =.

Try this,

<?php
    if ($_POST['city'] == 'london'){
      header('Location: london.html');
    }
    if ($_POST['city'] == 'manchester'){
      header('Location: manchester.html');
    }
    if ($_POST['city'] == 'conventry'){
      header('Location: coventry.php');
    }
    exit;
 ?>

Check the below link for more info. https://www.tutorialspoint.com/php/php_decision_making.htm

Saravanan Sachi
  • 2,572
  • 5
  • 33
  • 42
0

If you are just using the value in the location as a straight swap of destination - you don't need any ifs at all - just put the variable in the location:

<?php
  $city = $_POST['city'];
  header('Location:' . $city . '.html');
  exit;
?>

//$city = london;
//header('Location:london.html');

If you have different file extensions - eg the .html and .php that you show here - then you can use a switch statement to reduce the if statements:

   <?php
      $city = $_POST['city'];

      switch ($city) {
        case "london":
            header('Location: london.html');
            break;
        case "manchester":
            header('Location: manchester.html');
            break;
        case "coventry":
            header('Location: coventry.php');
            break;
        default:
            echo "Your destination did not match any of our available cities";
      }
   exit;
    ?>
gavgrif
  • 15,194
  • 2
  • 25
  • 27