我正在尝试以渐进的顺序获取用户输入,这导致通过电子邮件发送该输入。通过电子邮件发送是另一个我尚未解决的问题,所以并不是真正担心的问题。 我遇到困难的部分是一旦用户进入“发送电子邮件?” (是/否)单选按钮,该问题的输入未正确处理。 我通过使用单独的php文件作为表单操作,但仍然得到与emailName,emailAddress和emailMsg不存在相关的错误(“通知:未定义的索引...”)。 此外,我仍然需要能够进一步使用$ _POST [athletes]数组,但我猜测它在变量范围之外。 所以为了把这些结合在一起,我真的问了几个问题:
1)如何让所有表单在同一个文件中一起工作?
2)当程序实际超过“发送电子邮件?”时单选按钮当我使用单独的php文件作为表单操作时,为什么我会得到未定义的索引错误?
3)当我尝试在代码中进一步使用运动员[]数组时,为什么会出现错误?我应该以某种方式将数组值传递给代码的那一部分吗?
用户为解决问题而采取的确切步骤是:
选择一个或多个运动员复选框,然后单击“显示选择”按钮。
为“发送电子邮件”选择“是”?然后单击“提交”按钮。
出于某种原因重新启动代码。
非常感谢任何帮助。此外,这是我的第一篇文章,如果我根据网站礼仪错误地提出问题,那就很抱歉。 我也为长代码片段道歉,但我不确定哪些部分可能导致这不正确。
<b><h1><center>Athelete Selection Screen</center></h1></b>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Athletes Available: </legend>
<input type="checkbox" id="student1"
name="athletes[]" value="Student1 Test">
<label for="student1">Student1 Test</label><br/>
<font color="grey">Football - Running back</font><br/>
<p>
<input type="checkbox" id="student2"
name="athletes[]" value="Student2 Test">
<label for="student1">Student2 Test</label><br/>
<font color="grey">Soccer - Left Forward</font><br/>
</p>
<p>
<input type="checkbox" id="student3"
name="athletes[]" value="Student3 Test">
<label for="student1">Student3 Test</label><br/>
<font color="grey">Baseball - Pitcher/Left Outfield</font><br/>
</p>
</fieldset>
<p>
<?php echo("\t\t\t\t\t"); ?><button type="submit" name="submit" value="submit">Display Selection(s)</button>
</p>
</form>
<fieldset>
<legend>Athletes You Selected: </legend>
<?php
if (!empty($_POST['athletes']))
{
echo "<ul>";
foreach($_POST['athletes'] as $value)
{
echo "<li>$value</li>";
}
echo "</ul>";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<fieldset>
<legend>Send Email? </legend>
<input type="radio" id="Yes"
name="radioSendMsg[]" value="Yes">
<label for="student1">Yes</label>
<p>
<input type="radio" id="No"
name="radioSendMsg[]" value="No">
<label for="student1">No</label><br/>
</p>
<button type="submit" name="submitRadio" value="submit">Submit</button>
</p>
</form>
<?php
if (!empty($_POST['radioSendMsg']))
{
foreach($_POST['radioSendMsg'] as $radioMsg)
{
if($radioMsg == "Yes")
{
echo "\tPlease enter information regarding the email to be sent: ";
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST">
<p>
<label for="emailName"> Name: </label><br/>
<input type="text" size="25" id="emailName" name="emailName" />
</p>
<p>
<label for="emailAddress">E-mail Address: </label></br>
<input type="text" size="25" id="emailAddress" name="emailAddress" />
</p>
<p>
<textarea id="emailMsg" name="emailMsg" cols="30" rows="5"></textarea>
</p>
<button type="submit" name="emailSubmit" value="send">Send Message</button>
</form>
<?php
$msg = "Name: ".$_POST['emailName']."\n";
$msg.= "E-Mail: ".$_POST['emailAddress']."\n";
$msg.= "Message: ".$_POST['emailMsg']."\n";
$msg.= "<ul>";
foreach($_POST['athletes'] as $value)
{
$msg.= "<li>$value</li>\n";
}
$msg.= "</ul>";
$emailRecipient = "sjzerbib@gmail.com";
$emailSubject = "Athlete Selection Submission";
$emailHeaders = "From: Sebastien\n"."Reply-To: ".$_POST['emailAddress'];
mail($emailRecipient,$emailSubject,$msg,$emailHeaders);
echo "Message sent: \n".$msg;
}
else
{
?> <p /> <?php
echo "\n\nNo email will be sent for your last athlete selection.";
?>
<br/>Please click <a href="http://localhost/CheckFormTest.html">here</a>
to return to the Athlete selection screen.
<?php
}
}
}
}
答案 0 :(得分:1)
提交表单时,仅包含该表单中包含的控件。例外是将form
属性设置为已提交表单的id
值的成功控件。
所以,鉴于你有类似的东西:
<form id="form-1" method="post">
<input type="text" name="first-input" />
</form>
<input type="text" name="second-input" />
唯一要提交的值是first-input
。如果您将表单属性添加到second-input
:
<input type="text" name="second-input" form="form-1" />
然后提交表单将包含两个值。遗憾的是,不完全支持表单属性(IE and Edge have no support)。
当然,你的标记无效,所以这是一个有争议的问题。首先,you cannot nest a form within a form。浏览器如何响应违反该规则的标记取决于它的供应商,但根据我的经验有些不可预测。您还使用了已弃用的代码(<font>
和<center>
不再有效)并且嵌套元素不正确(<h1>
是块级元素,而<b>
是内联的)。
如果你每次都做一次完整的提交(所以页面被提交给自己然后重新加载),那么只需使用某种条件来只渲染依赖控件,如果前面的表单提交成功:
<?php
$canDoNextStep = !empty($_POST['input-1']);
?>
<form id="form-1" method="post">
<input type="text" name="first-input" />
<?php if(canDoNextStep): ?>
<input type="text" name="second-input" />
<?php endif; ?>
</form>
最后,当您的浏览器解析并显示HTML时,(大部分)会忽略空格,因此您可能会丢失字符串中的\t
和\n
值,除非您担心标记的方式查看是否有人在使用您的表单时选择查看来源。