dhyy dhyy - 2 months ago 6
HTML Question

How to redirect by drop down option to different actions in the same php file on submit?

Hi I am new in this area and the question might sounds ambiguous... What I am trying to achieve is, I want to search for a property entry with keywords of suburb and property type. So when I type in a keyword, such as "House", and choose the "property type" option in the drop down menu and click submit, the file will take action to give me the searched results.

Here's the code to give you a better picture


search.php




if(!isset($_POST['key']))
{
?>
<center><h2><font face="verdana">Search For Property </font></h2>

<form method="post" id="form_search">
Enter a keyword <input ="text" name="key"><br><br>

<select id ="searchOpt" form = "form_search" >
<option selected disabled> Search by...</option>
<option value = "type"> Property Type </option>
<option value = "suburb"> Suburb </option>
</select>

<input type="submit" id = "searchOpt" formaction="
<?php echo ($_POST["searchOpt"]="type")?'search.php?&Action=type':'search.php?&Action=suburb';?>">

</form>

</center>

<?php
}
else
{
switch($_GET["Action"])
{
case "suburb":
// show "suburb" results
break;
case "type":
// show "suburb" results
break;
}
}


As you can see I am trying to pass the option value to the back of "search.php?&Action=" in order to use the switch function below. However, no matter which option I chose, I am always getting the results from the case "type" but not the suburb. Does anyone know how to fix this? Can I use html codes to fix this problem only? If javascript or JQuery are to be resorted, can you go through the codes line by line for me? Much appreciated!

Answer

As far as I can see you don't need that tricky workaround with formaction if you simply send searchOpt with the form as Post parameter:

if(!isset($_POST['key']))
{
?>
 <center><h2><font face="verdana">Search For Property </font></h2>

<form method="post" id="form_search" action="search.php">
Enter a keyword <input type="text" name="key"><br><br>

<select name="searchOpt" >
  <option selected disabled> Search by...</option>
  <option  value = "type"> Property Type  </option>
  <option  value = "suburb"> Suburb  </option>
</select>

<input type="submit" id="searchOpt">

</form>

</center>

<?php
}
else
{
  switch($_POST["searchOpt"]) 
  {
      case "suburb":
           // show "suburb" results
           break;
     case "type":
          // show "suburb" results
          break;
  }
}