PHP - Write program to use the Location: header to redirect the browser to the appropriate URL

Requirements

Create a script that displays a form allowing the user to select one of three url - and then jumps to the relevant store based on the user's choice.

Hint

Use the Location: header to redirect the browser to the appropriate store after the form has been submitted.

You need to make sure that the script doesn't output anything before the Location: header:

Demo

<?php

if (isset($_POST[" submitButton" ])) {
  switch ($_POST[" store" ]) {
    case" .com" :
      header("Location: http://www.book2s.com/" );
      break;//from  w w w  .j  a  va  2s  .com
    case" .ca" :
      header("Location: http://www.google.ca/" );
      break;
  }
} else {
  displayForm();
}

function displayForm() {
?>
<html>
  <head>
    <title>Amazon Store Selector</title>
  </head>
  <body>
    Amazon Store Selector
    <form action="" method="post">
      <div>
        <label for="store">Choose your Amazon store:</label>
        <select name="store" id="store" size="1">
          <option value=".com">book2s.com</option>
          <option value=".ca">google.ca</option>
        </select>
        <div style="clear: both;">
          <input type="submit" name="submitButton" id="submitButton" value= " Visit Store" />
        </div>
      </div>
    </form>
<?php
}
?>
  </body>
</html>

Result

Related Exercise