3

This is my code for get database data to select box and i wanna get the seleceted value.I tries many ways but im missing something. help me

<form id="search" action="" method="post" >
   <select name="owner" id="owner">
   <?php 
      $sql = mysql_query("SELECT designation FROM designation");
      while ($row = mysql_fetch_array($sql)){
      echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
      }
      ?>
   </select>
   <input type="submit" value="Search">
</form>
Santosh Ram Kunjir
  • 1,074
  • 1
  • 12
  • 23
Niranjana668
  • 61
  • 2
  • 4
  • 12

5 Answers5

2

As you didn't specify an action for your form, the default will be to send the post values to the same page.

See this for more information about action value.

So, in the same page you have the form, you should add a

if(isset($_POST['owner']))
{
    // Do some stuff
}
else
{
    // Print the form
}
Community
  • 1
  • 1
Kethryweryn
  • 639
  • 4
  • 11
1

First make sure to include the action. Secondly to get a POST request of a select tag all you have to do is the following:

$_POST["owner"];
tabone
  • 172
  • 1
  • 5
  • Action can be empty, there is no need to fill it. And what you mean by `POST request of a select tag`? Maybe `value of field sent using POST`? – Elon Than Sep 03 '13 at 09:16
0

$_POST['owner'] contains the value of select box once you submit the form.And $_POST contains all the value of input elements you submitted via the form.if you print_r($_POST); it will show you all the values submitted through the form.

If you

echo $_POST['owner'];//Will display the value of your selected value in the select box.
웃웃웃웃웃
  • 11,829
  • 15
  • 59
  • 91
0
<form id="search" action="" method="post" >
            <select name="owner" id="owner">
            <?php 
             $owner="rejayi"
            $sql = mysql_query("SELECT designation FROM designation");
            while ($row = mysql_fetch_array($sql)){
               if($row['designation'] ==  $owner){
     echo '<option value="'.$row['designation'].'" selected="selected">'.$row['designation'].'</option>';
               }else{
           echo '<option value="'.$row['designation'].'">'.$row['designation'].'</option>';
               }
            }
            ?>
            </select>
            <input type="submit" value="Search">
            </form>
Rejayi CS
  • 1,034
  • 1
  • 10
  • 22
0

Put Double quotes (") outside and single quotes (') inside

eg:

echo "<option value='".$row['designation']."'>".$row['designation']."</option>";