我的表单中有一个带有“是”或“否”选项的下拉菜单,如果用户选择“否”,则会出现一个隐藏的Div,其中带有供用户填写的文本框。如果用户单击“是”,他们只是继续下一个问题。在用户选择“是”而不显示隐藏div的问题上,我收到一个未定义的索引错误。
这是我的index.php,其中包含表单和隐藏的div
<form id="newForm" action="" method="post">
<div id="hiddenQuestion1">
<strong>1. Are the Team Players Wearing Gloves for the correct procedure?</strong>
<select id="questions1" name="question1" onchange="showfield1(this.options[this.selectedIndex].value)">
<option value=""></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
<br><div id="div1"></div><br>
<!--Function to display text box if users selects no-->
<script type="text/javascript">
function showfield1(name){
if(name=='No')
document.getElementById('div1').innerHTML='Please provide a reason: <input type="text" id="reason1" name="reason1" size="43" /> Add attachment (Optional): <input type="file" id="attachmentFile1" name="attachmentFile1" class="InputBox">';
else
document.getElementById('div1').innerHTML='';
}
</script>
<div id="hiddenQuestion2" style="display:none">
<strong>2. Are Team Players using the ESD station and signing daily sheet?</strong>
<select id="questions2" name="question2" onchange="showfield2(this.options[this.selectedIndex].value)">
<option value=""></option>
<option value="Yes">Yes</option>
<option value="No">No</option>
</select>
</div>
<br><div id="div2"></div><br>
<!--Function to display text box if users selects no-->
<script type="text/javascript">
function showfield2(name){
if(name=='No')
document.getElementById('div2').innerHTML='Please provide a reason: <input type="text" id="reason2" name="reason2" size="43" /> Add attachment (Optional): <input type="file" id="attachmentFile2" name="attachmentFile2" class="InputBox">';
else
document.getElementById('div2').innerHTML='';
}
</script>
这是我的post.php页面
$reason1 = $_POST['reason1'];
$attachment1 = $_FILES['attachmentFile1']["name"];
$question2 = $_POST['question2'];
if($question2 ==''){
$data['error'] = "question 2 missing";
echo json_encode($data);
exit();
}
$reason2 = $_POST['reason2'];
$attachment2 = $_FILES['attachmentFile2']["name"];
$question3 = $_POST['question3'];
if($question3 ==''){
$data['error'] = "question 3 missing";
echo json_encode($data);
exit();
}
因此,如果用户对问题1选择“是”,对问题2选择“否”,则会在post.php页面中收到如下所示的错误“未定义索引:原因2” ...
"<br />
<b>Notice</b>: Undefined index: reason1 in
<b>D:\inetpub\wwwroot\SMT_24_Point_Check\newAudit.php</b> on line <b>17</b>
<br />
<br />
<b>Notice</b>: Undefined index: attachmentFile1 in
<b>D:\inetpub\wwwroot\SMT_24_Point_Check\newAudit.php</b> on line <b>18</b>
<br />
<br />
答案 0 :(得分:3)
您会收到“未定义索引”错误,因为如果答案为“否”,那么您甚至不会在DOM中生成输入,因此该post变量将不存在。快速解决方法如下:
而不是像这样的行
$reason2 = $_POST['reason2'];
尝试:
if(isset($_POST['reason2'])) {
$reason2 = $_POST['reason2'];
} else {
$reason2 = '';
}
或相同的东西,但以更短,更优雅的方式:
$reason2 = ($_POST['reason2']) ? $_POST['reason2'] : ''
对此的另一种解决方案将始终包括HTML输入,但是最初使用display: none;
隐藏它-然后根据用户的答案使其可见。这样,$_POST['reason2']
将始终存在,如果未设置,则为空。
答案 1 :(得分:2)
这是当您为选项1 选择是时发生的,隐藏字段reason1
没有任何值,因此您可以在post.php
使用isset()
函数进行的拳头检查是reason1
是否具有值?
if(isset($_POST['reason1'])) {
$reason1 = $_POST['reason1'];
} else {
$reason1 = 0; `//Better to pass a value in here for future refernces`
}
然后,当您将No
选择为 option2 时,如果将其保留为空(,我会看到您将i编写为可选),以便它也有一定的价值。这样您就可以像对$reason1
那样做。
if(isset($_POST['attachmentFile1'])) {
$attachmentFile1= $_POST['attachmentFile1'];
} else {
$attachmentFile1= 0; `//Better to pass a value in here for future refernces`
}
谢谢