javascript - 从下拉列表html php中选择项目时显示文本框?

标签 javascript php html

 <div class="control-group">
              <label class="control-label">
                <span class="red">*</span>Camera name</label>
              <div class="controls">
                <select name="cam_name" class="span3" id = "ddlPassport" onchange = "ShowHideDiv()">
                  <option>Select camera:</option>
                  <option>canon</option>
                  <option>nicon</option>
                  <option>sony</option>
                  <option>pentex</option>
                  <option>olympus</option>
                  <option>others</option>
                </select>
              </div>
            </div>
            <div class="control-group" id="dvPassport" style="display: none">
              <label class="control-label">Your camera name:</label>
              <div class="controls">
                <input type="text" placeholder="Enter model" name="cam_name" class="input-xlarge">
              </div>
            </div>


javascript

<script type="text/javascript">
function ShowHideDiv() {
    var ddlPassport = document.getElementById("ddlPassport");
    var dvPassport = document.getElementById("dvPassport");
    dvPassport.style.display = ddlPassport.value == "others" ? "block" : "none";
}




PHP代码

$camname = $_POST['cam_name'];// user name
if(empty($camname)){

      $errMSG = "Please enter camera name";

    }


当我从下拉列表中选择“其他”时...效果很好
但是问题是当我选择canon或nicon else时...隐藏的div的输入标签在与if(empty($ camname))一起咀嚼时在PHP代码中给出了错误..

最佳答案

选择和输入标签的名称不同。并添加选项值是一种很好的做法

<select name="cam_name" class="span3" id = "ddlPassport" onchange = "ShowHideDiv()">
<option value="">Select camera:</option>  // null
<option value="canon">canon</option>
<option value="nicon">nicon</option>
<option value="sony">sony</option>
<option value="pentex">pentex</option>
<option value="olympus">olympus</option>
<option value="others">others</option>
</select>

// give a new name for this input
<input type="text" placeholder="Enter model" name="other_cam_name" class="input-xlarge">


PHP代码:

 $camname = $_POST['cam_name'];// cam name
 $other_camname = $_POST['other_cam_name'];// other cam name
 // if both select and input values 
 if(empty($camname) || ($camname =='others' && empty($other_camname))){
    echo $errMSG = "Please enter camera name";
 }else{
    echo $cam_name =  ($camname =='others') ?  $other_camname : $camname;
 }

关于javascript - 从下拉列表html php中选择项目时显示文本框?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46714216/

相关文章:

javascript - jQuery动画循环回调

javascript - 使用不可变帮助器在Redux中删除数组的最后一个元素,而无需知道数组的长度

php - MySQL空查询

javascript - PM2 管理 PHP 脚本 - 集群模式

javascript - SASS错误导致奇怪的Gulp崩溃

javascript - github 页面提供 javascript/d3 可视化

php - 将子类映射为其扩展父类

javascript - 如何根据浏览器大小调整 div 的高度

javascript - 在 Dropzone.js 中将 ID 添加到预览 div

html - 如何在Image onmouseover事件上显示两个链接?