如何检查是否在php中选择了一个下拉列表,multiple = multiple?

时间:2016-12-13 08:29:21

标签: php validation

我有一个multiple=multiple下拉列表,我想检查用户是否选择了使用PHP代码的选项。如果没有,那么我想显示一条错误消息。我怎么能这样做?

<select name="ddlskill" multiple="multiple">
    <option value="Cake PHP">Cake PHP</option>
    <option value="Database">Database</option>
    <option value="hello">hello</option>
    <option value="Java">Java</option>
    <option value="Jquery">Jquery</option>
    <option value="JSON">JSON</option>
    <option value="JSP">JSP</option>
    <option value="Mysql">Mysql</option>
    <option value="PHP">PHP</option>
    <option value="xyz">xyz</option>
    <option value="XYZ2">XYZ2</option>                
</select>

3 个答案:

答案 0 :(得分:1)

你应该声明你的html:

<select name="ddlskill[]" multiple="multiple">
    <option value="Cake PHP">Cake PHP</option>
    <option value="Database">Database</option>
    <option value="hello">hello</option>
    <option value="Java">Java</option>
    <option value="Jquery">Jquery</option>
    <option value="JSON">JSON</option>
    <option value="JSP">JSP</option>
    <option value="Mysql">Mysql</option>
    <option value="PHP">PHP</option>
    <option value="xyz">xyz</option>
    <option value="XYZ2">XYZ2</option>   
</select>

然后在PHP中,您将获得一系列值:

if( !empty($_POST['ddlskill']) ){
    print_r($_POST['ddlskill']);
    // [PHP, JSP, Java, ...]
}

答案 1 :(得分:1)

也许有帮助,

  <?php 
    if( isset($_POST) && !empty($_POST['ddlskill'])){
    echo "<pre>";print_r($_POST['ddlskill']);
        // [PHP, JSP, Java, ...]
    }
    ?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
    <option value="Cake PHP">Cake PHP</option>
    <option value="Database">Database</option>
    <option value="hello">hello</option>
    <option value="Java">Java</option>
    <option value="Jquery">Jquery</option>
    <option value="JSON">JSON</option>
    <option value="JSP">JSP</option>
    <option value="Mysql">Mysql</option>
    <option value="PHP">PHP</option>
    <option value="xyz">xyz</option>
    <option value="XYZ2">XYZ2</option>   
</select>
<button type="submit"> Submit </button>
</form>

答案 2 :(得分:0)

  <?php 
    if(!empty($_POST['submit'])){
      if(is_array($_POST['ddlskill']))
      {
       foreach($_POST['ddlskill'] as $key=>$value)
       {
       print_r($value);
       }
      }
      else
      {
       print_r($_POST['ddlskill']);
      }
    }
    ?>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']);?>" method="post">
<select name="ddlskill[]" multiple="multiple">
    <option value="Cake PHP">Cake PHP</option>
    <option value="Database">Database</option>
    <option value="hello">hello</option>
    <option value="Java">Java</option>
    <option value="Jquery">Jquery</option>
    <option value="JSON">JSON</option>
    <option value="JSP">JSP</option>
    <option value="Mysql">Mysql</option>
    <option value="PHP">PHP</option>
    <option value="xyz">xyz</option>
    <option value="XYZ2">XYZ2</option>   
</select>
<button type="submit" name="submit"> Submit </button>
</form>