【问题标题】:How to filed form fields based upon user selection in php?如何根据用户在 php 中的选择提交表单字段?
【发布时间】:2020-11-03 16:31:44
【问题描述】:

我已经构建了一个表单,但我有点卡在我有一个“是”或“否”字段的地方,并且在选择“是”时,一些字段应该出现,否则如果用户选择“否”,这些字段应该被隐藏。我不知道该怎么做。

代码如下:

<div class="container">
            <h1>Research</h1>
            <div class="form-group col-md-3">
            <p>Do you have previous research experience? If yes, answer Questions...</p>
               <input type="checkbox" name="yes" value="yes">
                <label for="yes">Yes</label><br>
                 <input type="checkbox" name="no" value="no">
               <label for="no">No</label><br>
            </div> 

            <div class="form-group col-md-6" style="display: none">
                <label for="lengthinresearch">Length of time in Research</label>
                <input type="text" class="form-control" id="lengthinreseasrch" name="lengthinresearch">
            </div>
            <div class="form-group col-md-3">
            <p>Type of Research</p>
               <input type="checkbox" name="basic" value="basic">
                <label for="basic">Basic</label><br>
                 <input type="checkbox" name="clinical" value="clinical">
               <label for="clinical">Clinical</label><br>
            </div>
            <div class="form-group col-md-6">
                <label for="researchinstitution">Research Institution</label>
                <input type="text" class="form-control" id="researchinstitution" name="researchinstitution">
            </div>
            <div class="form-group col-md-6">
                <label for="researchmentor">Research Mentor</label>
                <input type="text" class="form-control" id="researchmentor" name="researchmentor">
            </div>
            </div>

【问题讨论】:

  • 为什么不使用 javascript 来处理逻辑,尝试为每个输入的 div 标签提供 id,并在选择是或否时调用 javascript 函数并隐藏/取消隐藏输入部分。
  • 我很想这样做,但没有用。我遵循了以下问题:stackoverflow.com/questions/40581263/…你能告诉我怎么做吗?

标签: php forms hidden-field


【解决方案1】:

这里是新手,也在学习。希望我能遵守约定。这是 Gopi 建议的一个例子。我知道您要求使用 PHP,但仅使用 PHP,一旦加载,您就无法更改页面。

<script>

    function hideDiv(){

        let checked = document.getElementById("experience").checked

        if (checked) {
            document.getElementById("id_of_div_to_hide").style.display="block"
        }
        else {
            document.getElementById("id_of_div_to_hide").style.display="none"
        }
    }

</script>

<div class="form-group col-md-3">
    <p>Do you have previous research experience? If yes, answer Questions...</p>
    <input onclick="hideDiv()" type="checkbox" id="experience" name="experience" >
    <label for="yes">Yes</label><br>

</div>

<div id="id_of_div_to_hide" class="form-group col-md-6" style="display: none">
    <label for="lengthinresearch">Length of time in Research</label>
    <input type="text" class="form-control" id="lengthinreseasrch" name="lengthinresearch">
</div>

【讨论】:

  • 这行得通,但我想要相反的情况,比如当他们选择“是”时,才会出现一些字段,否则应该隐藏这些字段。
  • 我已经编辑过了。现在它以未选中“是”和隐藏示例字段开始。当您选中“是”时,将出现该字段。我还删除了“否”复选框,因为复选框不是相互排斥的。如果您想显示“是”和“否”,您可以使用单选按钮而不是复选框。
  • 好的,我将其更改为单选按钮。但是在这种情况下,当我刷新页面时,字段就在那里,然后当我选择和取消选择是时,它会出现和消失。我希望它们被隐藏起来。我不得不对这些列使用 style="display: none",我将在问题中编辑上面的代码。谢谢
  • 我不确定我是否理解您的问题。在我的示例中,当您刷新页面时,示例字段开始隐藏。
【解决方案2】:

这可以使用JQuery来完成,看看下面的例子:

HTML:

<input id="boxid" type="checkbox"><label for="boxid">not checked</label>

JQuery:

$('#boxid').click(function() {
  if ($(this).is(':checked')) {
    $(this).siblings('label').html('checked');
  } else {
    $(this).siblings('label').html(' not checked');
  }
});

http://jsfiddle.net/zpzxM/

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-10-04
    • 2011-12-31
    • 2015-11-04
    • 2010-09-23
    • 2021-04-14
    相关资源
    最近更新 更多