简体   繁体   English

PHP从选择框中获取选择的值?

[英]PHP get selected value from select box?

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>

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
}

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请求,您需要做的是:

$_POST["owner"];

$_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); $_POST['owner']包含选择框的值在您提交form.And $_POST包含你通过你的form.if提交的输入元素的所有值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.
<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>

Put Double quotes (") outside and single quotes (') inside 将双引号(“)放在外面,将单引号(')放在里面

eg: 例如:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM