我对php很新,我正在尝试编写联系表单/问卷
我需要表单来拉出单选按钮的结果,例如
员工姓名:伊恩马修斯 马克:(他们点击的单选按钮) 评论:他们在文本框中留下了什么评论。
但如果填写了其他名称,则使用上述详细信息创建一个新行
员工姓名:伊恩马修斯 马克:(他们点击的单选按钮) 评论:他们在文本框中留下了什么评论。
员工姓名:Ciara Maguire 马克:(他们点击的单选按钮) 评论:他们在文本框中留下了什么评论。
我正在尝试使用" foreach"但不要以为我正确使用它
希望有人可以提供帮助
由于
目前我有这段代码
<?php
$EmailFrom = "";
$EmailTo = "";
$Subject = "Questionnaire";
$EmpName = Trim(stripslashes($_POST['EmployeeName']));
$Radio = Trim(stripslashes($_POST['Radio']));
$Comment = Trim(stripslashes($_POST['Com']));
foreach ($_POST ['Radio'] as $value) {
$Body = "$EmpName, $Radio, $Comment\n";
}
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "top work";
}
else{
print "you have a error";
}
?>
&#13;
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td> </td>
<td>
<h4>Excellent</h4>
</td>
<td>
<h4>Very Good</h4>
</td>
<td>
<h4>Good</h4>
</td>
<td>
<h4>Satisfactory</h4>
</td>
<td>
<h4>Poor</h4>
</td>
<td>
<h4>N/A</h4>
</td>
</tr>
<tr>
<td height="30">
<label>Ian Matthews
<input type="hidden" name="EmployeeName" value="Ian Matthews" />
</label>
</td>
<td align="center">
<input type="radio" name="Radio" value="10-Excellent" />
</td>
<td align="center">
<input type="radio" name="Radio" value="7-VeryGood" />
</td>
<td align="center">
<input type="radio" name="Radio" value="4-Good" />
</td>
<td align="center">
<input type="radio" name="Radio" value="0-Satisfactory" />
</td>
<td align="center">
<input type="radio" name="Radio" value="-5-Poor" />
</td>
<td align="center">
<input type="radio" name="Radio" value="N/A" />
</td>
</tr>
<tr>
<td height="30">
<label class="colourGrey">Comments:</label>
</td>
<td colspan="6" align="center">
<input class="teamCom" type="text" name="Com" />
</td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td colspan="7" class="BorderLine"></td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td> </td>
<td>
<h4>Excellent</h4>
</td>
<td>
<h4>Very Good</h4>
</td>
<td>
<h4>Good</h4>
</td>
<td>
<h4>Satisfactory</h4>
</td>
<td>
<h4>Poor</h4>
</td>
<td>
<h4>N/A</h4>
</td>
</tr>
<tr>
<td height="30">
<label>Ciara Maguire
<input type="hidden" name="EmployeeName" value="Ciara Maguire" />
</label>
</td>
<td align="center">
<input type="radio" name="Radio" value="10-Excellent" />
</td>
<td align="center">
<input type="radio" name="Radio" value="7-VeryGood" />
</td>
<td align="center">
<input type="radio" name="Radio" value="4-Good" />
</td>
<td align="center">
<input type="radio" name="Radio" value="0-Satisfactory" />
</td>
<td align="center">
<input type="radio" name="Radio" value="-5-Poor" />
</td>
<td align="center">
<input type="radio" name="Radio" value="N/A" />
</td>
</tr>
<tr>
<td height="30">
<label class="colourGrey">Comments:</label>
</td>
<td colspan="6" align="center">
<input class="teamCom" type="text" name="Com" />
</td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td colspan="7" class="BorderLine"></td>
</tr>
</table>
&#13;
答案 0 :(得分:2)
首先,您为两名员工使用相同的输入名称。您必须设置不同的名称:
<input type="hidden" name="EmployeeName1" value="Ian Matthews" />
<input type="hidden" name="EmployeeName2" value="Ciara Maguire" />
这与所有输入(Com1和Com2,radio1和radio2,..)
在你的php文件中,你必须做类似
的事情$Body = "";
$numEmployees = 2; // or the number you put
// Each iteration per employee, accesing to his form data
for ($i=1; $i<=$numEmployees; $i++)
{
$EmpName = Trim(stripslashes($_POST['EmployeeName' .$i]));
$Comment = Trim(stripslashes($_POST['Com' .$i]));
$Radio = Trim(stripslashes($_POST['radio' .$i]));
$Body .= "$EmpName, $Radio, $Comment\n"; // Note the dot in ".="
}
然后
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
当然,你有电子邮件和电子邮件从空,但我想你在最终版本中填写它们。 我没有执行此版本,但我希望可以帮助您理解您的错误。
答案 1 :(得分:0)
$_POST['Radio']
包含单个值,而您希望通过多个值发送。为此,最好的方法是将输入分配给数组:
<input name="Radio[]" value="Lorem" />
<input name="Radio[]" value="ipsum" />
<input name="Radio[]" value="dolor" />
<input name="Radio[]" value="sit" />
<input name="Radio[]" value="amet" />
然后,您可以通过将它们作为数组访问来检索结果:
$_POST['Radio'][0] == 'Lorem'
$_POST['Radio'][4] == 'amet'
最后,您每次都在循环中覆盖$ Body的赋值。而不是$Body = "$EmpName, $Radio, $Comment\n";
,您需要$Body .= "$EmpName, $Radio, $Comment\n";
。注意等于之前的点。这会附加一个变量而不是覆盖它。
假设除了每个POST的许多单选按钮响应之外,您只有一名员工提交了一条评论,那么您可以使用以下内容:
foreach ($_POST ['Radio'] as $value) {
$Body .= "$EmpName, $value, $Comment\n";
}
希望这有帮助!
答案 2 :(得分:0)
我修改了你的代码 -
<form method="post" >
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td> </td>
<td>
<h4>Excellent</h4>
</td>
<td>
<h4>Very Good</h4>
</td>
<td>
<h4>Good</h4>
</td>
<td>
<h4>Satisfactory</h4>
</td>
<td>
<h4>Poor</h4>
</td>
<td>
<h4>N/A</h4>
</td>
</tr>
<tr>
<td height="30">
<label>Ian Matthews
<input type="hidden" name="EmployeeName1" value="Ian Matthews" />
</label>
</td>
<td align="center">
<input type="radio" name="Radio1" value="10-Excellent" />
</td>
<td align="center">
<input type="radio" name="Radio1" value="7-VeryGood" />
</td>
<td align="center">
<input type="radio" name="Radio1" value="4-Good" />
</td>
<td align="center">
<input type="radio" name="Radio1" value="0-Satisfactory" />
</td>
<td align="center">
<input type="radio" name="Radio1" value="-5-Poor" />
</td>
<td align="center">
<input type="radio" name="Radio1" value="N/A" />
</td>
</tr>
<tr>
<td height="30">
<label class="colourGrey">Comments:</label>
</td>
<td colspan="6" align="center">
<input class="teamCom" type="text" name="Com1" />
</td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td colspan="7" class="BorderLine"></td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td> </td>
<td>
<h4>Excellent</h4>
</td>
<td>
<h4>Very Good</h4>
</td>
<td>
<h4>Good</h4>
</td>
<td>
<h4>Satisfactory</h4>
</td>
<td>
<h4>Poor</h4>
</td>
<td>
<h4>N/A</h4>
</td>
</tr>
<tr>
<td height="30">
<label>Ciara Maguire
<input type="hidden" name="EmployeeName2" value="Ciara Maguire" />
</label>
</td>
<td align="center">
<input type="radio" name="Radio2" value="10-Excellent" />
</td>
<td align="center">
<input type="radio" name="Radio2" value="7-VeryGood" />
</td>
<td align="center">
<input type="radio" name="Radio2" value="4-Good" />
</td>
<td align="center">
<input type="radio" name="Radio2" value="0-Satisfactory" />
</td>
<td align="center">
<input type="radio" name="Radio2" value="-5-Poor" />
</td>
<td align="center">
<input type="radio" name="Radio2" value="N/A" />
</td>
</tr>
<tr>
<td height="30">
<label class="colourGrey">Comments:</label>
</td>
<td colspan="6" align="center">
<input class="teamCom" type="text" name="Com2" />
</td>
</tr>
<tr>
<td height="10" colspan="7"></td>
</tr>
<tr>
<td colspan="7" class="BorderLine"></td>
</tr>
</table>
<input type="submit" value="Send Feedback" />
</form>
<?php
if(isset($_POST)) {
$EmailFrom = "";
$EmailTo = "";
$Subject = "Questionnaire";
$Body = "";
$noParaPerEmployee = 3;
$n = 0;
foreach ($_POST as $key=> $value) {
//echo "$key => $value <br/>";
//$Body .= "$EmpName, $Radio, $Comment\n";
$Body .= "$value , ";
$n++;
if($n===3) {
$Body = substr($Body,0, -2); // get rid of last comma..
$Body .= "\n";
$n = 0;
}
}
//echo "$Body <br/>\n";
$success = mail($EmailTo, $Subject, $Body, "From: <$EmailFrom>");
if ($success){
print "top work";
}
else{
print "you have a error";
}
}
?>