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>
5
  • 2
    Try <?php echo $_POST['owner']; ?> Commented Sep 3, 2013 at 9:08
  • For future reference, you can do this to see all form data sent to the script: print_r($_POST);. Commented Sep 3, 2013 at 9:09
  • You have designation field in database table ? Commented Sep 3, 2013 at 9:12
  • where do you wish to select the value, at the back-end or at the front-end? Commented Sep 3, 2013 at 9:14
  • possible duplicate of Using $_POST to get select option value from HTML Commented Sep 21, 2015 at 8:21

5 Answers 5

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
}
Sign up to request clarification or add additional context in comments.

Comments

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"];

1 Comment

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?
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.

Comments

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>

Comments

0

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

eg:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.