如果php中的select值为null,如何显示输入字段

时间:2016-03-21 02:53:32

标签: php sql

当选择值为null时,如何显示我想显示输入字段以输入客户自己的选择。我是php的新手,所以任何人都可以建议我怎么做?这是我的参考代码。

<select class="form-control" name="outlet">
<option>Select Outlet</option>
<?php 
$values = explode(",",$camp_outlet);
foreach(explode(",",$camp_outlet) as $value){
?>
<option value="<?php echo "$value";?>">
<?php echo "$value";?>
</option>
<?php 
}
?>

1 个答案:

答案 0 :(得分:1)

    <?php 
        $theData = explode(",",$camp_outlet);
        if (count($theData) > 0 ){
            echo "<select class=\"form-control\" name=\"outlet\">";
            echo "<option>Select Outlet</option>";

            foreach($theData as $value){ // loop through the data and add the options to the select input
                echo "<option value=\"$value\">$value</option>";
            }

            echo "</select>";
         } else {
             echo "<input type=\"text\" class=\"form-control\" name=\"outlet\">";
         }
     ?>