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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18588241/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 17:51:42  来源:igfitidea点击:

PHP get selected value from select box?

phpmysql

提问by Niranjana668

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>

采纳答案by Kethryweryn

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

由于您没有为表单指定操作,因此默认设置是将帖子值发送到同一页面。

See thisfor 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
}

回答by tabone

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:

首先确保包含操作。其次,要获取 select 标签的 POST 请求,您只需执行以下操作:

$_POST["owner"];

回答by Rejayi CS

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

回答by Tanmay Gawankar

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

将双引号 (") 放在外面,将单引号 (') 放在里面

eg:

例如:

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

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

回答by Deepu

$_POST['owner']contains the value of select box once you submit the form.And $_POSTcontains 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.

$_POST['owner']包含提交表单后选择框的值。并$_POST包含您通过表单提交的输入元素的所有值。如果您print_r($_POST);将显示通过表单提交的所有值。

If you

如果你

echo $_POST['owner'];//Will display the value of your selected value in the select box.