如何根据 php 中的用户选择提交表单字段?
How to filed form fields based upon user selection in php?
我已经构建了一个表单,但我有点卡在这个有“是”或“否”字段的地方,选择“是”后,一些字段应该出现,否则如果用户选择“否”,这些字段应该被隐藏.我不知道该怎么做。
代码如下:
<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>
这可以使用 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');
}
});
新来的,也在学习。希望我能遵守惯例。这是 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>
我已经构建了一个表单,但我有点卡在这个有“是”或“否”字段的地方,选择“是”后,一些字段应该出现,否则如果用户选择“否”,这些字段应该被隐藏.我不知道该怎么做。
代码如下:
<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>
这可以使用 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');
}
});
新来的,也在学习。希望我能遵守惯例。这是 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>