如何使用 PHP 验证下拉列表

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

How can I use PHP to validate a drop down list

php

提问by Apulo Ohale Cosmas Lite

I want to validate a form using PHP. Below is the sample of my form:

我想使用 PHP 验证表单。以下是我的表格样本:

<form>
    <select name="select_box">
        <option value="0">Please Select</option>
        <option value="1">OPT 1</option>
        <option value="2">OPT 2</option>
    </select>
    <input type="submit" value="Go!" />
</form>

采纳答案by Vidyadhar

<?php 
if(isset($_REQUEST['select_box']) && $_REQUEST['select_box'] == '0') { 
  echo 'Please select a country.'; 
} 
?>

回答by Andreas Wong

In your php script where are you are submitting your form (in this case it's the same script that echoes out the form, since you aren't specifying actionand methodattribute to your <form>), you can get values of inputs by doing $_GET['name_of_input'], which in your case:

在您的 php 脚本中,您在哪里提交表单(在这种情况下,它与回显表单的脚本相同,因为您没有指定actionmethod归属于您的<form>),您可以通过执行 获取输入的值$_GET['name_of_input'],在您的情况下:

if(isset($_GET['select_box'])) { // do something with value of drop down

if(isset($_GET['select_box'])) { // do something with value of drop down

回答by egis

<?php
if ($_GET['select_box'] != '0') //form validation
{
 //your code here
}
?>