Page 1 of 1

want selected option from html form to link to a file

Posted: Fri May 09, 2008 1:14 pm
by abromell
Hi. This is probably a baby question but please help me. I have an html file that has a form for choosing a city from a drop down list of states/cities. When the city(option) is chosen, say for example, Phoenix, I want the php file to link to a html file Phoenix.html. I don't understand how to use the php file to take the information from the form, and process it, say, using an IF statement to link it to a different file (depending on the result of the IF statement). I highlighted the area that I think I need code. Any help will be appreciated. Thanks.

states.html file:

Code: Select all

 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns = "http://www.w3.org/1999/xhtml" xml:lang="en" lang = "en">
<head>
<meta http-equiv = "content-type" content = "text/html; charset=iso-8859-1">
<title> Select State And City </title>
</head>
<body>
<form action = "choose_family_city.php" method = "post">
<p><b>State and City: <select name = "city" size = "1"></b>
<optgroup label = "Arizona">
    <option>Phoenix</option>
</optgroup>
</select></p>
<div align = "center"> <input type = "submit" name = "submit" value = "Find " /> </div>>
</form>
</body>
</html>
 
choose_family_city.php file:

Code: Select all

 
<html>
<body>
<?php
$page_title = 'Family Cities';
require(states.html);
$city = $_POST['city'];
    if($city == 'Phoenix'){
[color=#FF0000]//what would go here to open Phoenix.html???[/color] }
?>
</body>
</html>
 

Re: want selected option from html form to link to a file

Posted: Fri May 09, 2008 1:57 pm
by flying_circus
You've got a couple of options. You can either include the contents of pheonix.html or you can redirect to pheonix.html.

Include:

Code: Select all

<html>
  <body>
    <?php
      $page_title = 'Family Cities';
      $city = (isset($_POST['city'])) ? $_POST['city'] : '';
      require_once("states.html");
      if(!empty($city) && $city == "Phoenix"){
        require_once("phoenix.html");
      }
    ?>
  </body>
</html>

Redirect:

Code: Select all

 
<?php
  $city = (isset($_POST['city'])) ? $_POST['city'] : '';
  if(!empty($city) && $city == "Phoenix"){
    header("location:phoenix.html");
  }
?>

As a side note, if you will need alot of in statements:
if $city == "Seattle";
if $city == "Portland";
if $city == "Los Angeles";

you should look into using a switch for readability:

Code: Select all

 
switch ($city) {
  case 'Seattle':
 
    break;
  case 'Portland':
 
    break;
  default:
  // No city Specified, redirect to an error page
    break;
}