我有一个表单,它是一个复选框列表。每个复选框旁边都有一个与复选框一起使用的文本字段。下面是我正在谈论的图像
红色的文字是name
属性。我的问题是基于用户检查标记,如何匹配配对文本字段而不做一堆if语句?
我知道如何发布复选框,然后只发布文本字段。我想我被困在如何配对结果。这就是我用$_POST
复选框
if (isset($_POST['family_medical_history']))
{
$_SESSION['patient_form']['family_medical_history'] = array();
foreach ($_POST['family_medical_history'] as $key => $value)
{
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST['family_medical_history'][$key];
}
}
else
{
$_SESSION['patient_form']['family_medical_history'] = "none";
}
示例HTML表单
<div class="form-group col-sm-4">
<label><input type="checkbox" name="family_medical_history[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes">
</div>
答案 0 :(得分:1)
您可以通过为每个复选框/关系对命名输入来为它们配对,以便为每个项目生成一个两个成员的数组。
<div class="form-group col-sm-4">
<label><input type="checkbox" name="rel_crossed_eyes[]" value="Crossed Eyes" /> Crossed Eyes</label>
</div>
<div class="form-group col-sm-4">
<input type="text" class="form-control" id="" name="rel_crossed_eyes[]">
</div>
在PHP中,它们将成为对,0元素等于复选框输入的值,1元素等于文本输入。您可以创建条件数组并循环遍历每个命名输入。您必须将if语句更改为其他语句,例如提交按钮:
$conditions = array("rel_blindness" => "Blindness", "rel_cataracts" => "Cataracts", "rel_crossed_eyes" => "Crossed Eyes", "rel_glaucoma" => "Glaucoma");
if (isset($_POST['submit'])){
foreach ($conditions as $key => $value){
if ($_POST[$key][0] == $value){//if the checkbox is checked
$_SESSION['patient_form']['family_medical_history'][$key] = $_POST[$key][1];
}
}
}