从 PHP 下拉框中获取 Selected 值

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10207671/
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-24 21:38:48  来源:igfitidea点击:

Get the Selected value from the Drop down box in PHP

php

提问by Subash

I am populating a Drop Down Box using the following code.

我正在使用以下代码填充下拉框。

<select id="select_catalog">
<?php 
$array_all_catalogs = $db->get_all_catalogs();
foreach($array_all_catalogs as $array_catalog){?>
<option value="<?= $array_catalog['catalog_key'] ?>"><?= array_catalog['catalog_name'] ?></option>  

Now how can I get the selected option value using PHP(I have the code to get the selected item using Javascriptand jQuery) because I want the selected value to perform a query in database.

现在如何使用PHP(我有使用Javascript和获取所选项目的代码)获取所选选项值,jQuery因为我希望所选值在数据库中执行查询。

Any help will be much appreciated. Thank you very much...

任何帮助都感激不尽。非常感谢...

回答by Manuel

You need to set a name on the <select>tag like so:

您需要在<select>标签上设置一个名称,如下所示:

<select name="select_catalog" id="select_catalog">

You can get it in php with this:

您可以通过以下方式在 php 中获取它:

$_POST['select_catalog'];

回答by Aleski

Couldnt you just pass the a name attribute? And wrap it in a form?

你不能只传递一个名称属性吗?并将其包装成表格?

<form id="form" action"do_stuff.php" method="post">
    <select id="select_catalog" name="select_catalog_query">
    <?php <<<INSERT THE SELECT OPTION LOOP >>> ?>
    </select>
</form>

And then look for $_POST['select_catalog_query']?

然后找$_POST['select_catalog_query']

回答by Shashank Saxena

Posting it from my project.

从我的项目中发布它。

<select name="parent" id="parent"><option value="0">None</option>
<?php
 $select="select=selected";
 $allparent=mysql_query("select * from tbl_page_content where parent='0'");
 while($parent=mysql_fetch_array($allparent))
   {?>
   <option value="<?= $parent['id']; ?>" <?php if( $pageDetail['parent']==$parent['id'] ) { echo($select); }?>><?= $parent['name']; ?></option>
  <?php 
   }
  ?></select>

回答by Iso

You have to give a nameattribute on your <select />element, and then use it from the $_POSTor $_GET(depending on how you transmit data) arrays in PHP. Be sure to sanitize user input, though.

您必须name在您的<select />元素上提供一个属性,然后从PHP 中的$_POSTor $_GET(取决于您传输数据的方式)数组中使用它。不过,一定要清理用户输入。