gpt4 book ai didi

javascript - 在隐藏的 div 中发布变量

转载 作者:行者123 更新时间:2023-11-28 04:43:54 26 4
gpt4 key购买 nike

我的表单中有一个带有"is"或“否”选项的下拉列表,如果用户选择“否”,则会出现一个隐藏的 Div 以及一个供用户填写的文本框。如果用户单击"is"他们只是继续下一个问题。我在用户选择"is"的问题上收到 undefined index 错误,其中隐藏的 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 选择"is",对问题 2 选择“否”,我会在 post.php 页面中收到错误“Undefined index: reason2”,如下所示...

"<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 />

最佳答案

当您为选项 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`
}

然后当您选择 Nooption2 并且如果您将其留空(我可以看到您将 i 写为可选的) 所以它也有一个值。所以你可以像这样为 $reason1 做同样的事情。

if(isset($_POST['attachmentFile1'])) {
$attachmentFile1= $_POST['attachmentFile1'];
} else {
$attachmentFile1= 0; `//Better to pass a value in here for future refernces`
}

谢谢

关于javascript - 在隐藏的 div 中发布变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56883121/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com