给定项目列表, 任务是使用PHP从选择框中检索多个选择的值。
在HTML中使用Multiple属性从下拉列表中选择多个值。在HTML中选择多个值取决于操作系统和浏览器。
- 对于窗口用户–按住+ CTRL键选择多个选项
- 对于Mac用户-按住Command键可选择多个选项
<
html>
<
body>
<
form method = "post" action = "name.php">
<
h4>
SELECT SUJECTS<
/h4>
<
!--Using multiple to select multiple value-->
<
select name = "subject" multiple size = 6>
<
option value = "http://www.srcmini.com/english">
ENGLISH<
/option>
<
option value = "http://www.srcmini.com/maths">
MATHS<
/option>
<
option value = "http://www.srcmini.com/computer">
COMPUTER<
/option>
<
option value = "http://www.srcmini.com/physics">
PHYSICS<
/option>
<
option value = "http://www.srcmini.com/chemistry">
CHEMISTRY<
/option>
<
option value = "http://www.srcmini.com/hindi">
HINDI<
/option>
<
/select>
<
input type = "submit" name = "submit" value = http://www.srcmini.com/Submit>
<
/form>
<
/body>
<
/html>
现在, 任务是从列表中检索或打印多个选定的值。使用表单方法和循环来检索PHP中的选定值。
【如何在php中获取选择框的多个选定值()】例子:
<
html>
<
body>
<
!--name.php to be called on form submission-->
<
form method = 'post'>
<
h4>
SELECT SUJECTS<
/h4>
<
select name = 'subject[]' multiple size = 6>
<
option value = 'http://www.srcmini.com/english'>
ENGLISH<
/option>
<
option value = 'http://www.srcmini.com/maths'>
MATHS<
/option>
<
option value = 'http://www.srcmini.com/computer'>
COMPUTER<
/option>
<
option value = 'http://www.srcmini.com/physics'>
PHYSICS<
/option>
<
option value = 'http://www.srcmini.com/chemistry'>
CHEMISTRY<
/option>
<
option value = 'http://www.srcmini.com/hindi'>
HINDI<
/option>
<
/select>
<
input type = 'submit' name = 'submit' value = http://www.srcmini.com/Submit>
<
/form>
<
/body>
<
/html>
<
?php//Check if form is submitted successfully
if (isset( $_POST ["submit" ]))
{
//Check if any option is selected
if (isset( $_POST [ "subject" ]))
{
//Retrieving each selected option
foreach ( $_POST [ 'subject' ] as $subject )
print "You selected $subject<
br/>
" ;
}
else
echo "Select an option first !!" ;
}
?>
输出如下:
文章图片
注意:该表格可以使用$ _GET方法。这取决于form方法=” ?” 值。
推荐阅读
- 如何在react-bootstrap中获取选择元素的值()
- 如何从PHP中的路径获取文件名()
- 如何在Java 8中从Stream获取ArrayList()
- 如何获得数据科学实习()
- LINUX下载编译FreeSwitch
- Harbor企业级镜像仓库安装部署
- Docker-compose搭建nginx负载均衡
- LINUX下载编译ldns
- jenkins安装与配置