0

我有一个带有复选框的基本表单,提交时将通过电子邮件发送给管理员(并复制给客户)。我的代码仅回显最后一个复选框(因此仅在电子邮件上显示一个复选框) - 我无法让它发送电子邮件中的所有复选框信息(即使在此处尝试了多个版本的代码之后)。

<form method="POST" name="contactform" action="include/contactstationcode.php"> 
<h1>Quick Contact Form</h1> 
<p>
<section id="nameLabel" class="labelClass"><label id="Name">Full Name: </label><input name="name" id="name" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="Student ID">Student ID: </label><input name="studentid" id="studentid" size="8" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="Email">Email: </label><input name="email" id="email" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="ContactNumber">Contact Number: </label><input name="contactnumber" id="contactnumber" size="30" class="inputField" type="text"><br></section>
<section id="nameLabel" class="labelClass"><label id="interests">Interests: <input type="checkbox" name="check_list[]" value="Presenting " checked> Presenting<br><input type="checkbox" name="check_list[]" value="Producing "> Producing<br><input type="checkbox" name="check_list[]" value="Audio Production ">Audio Production<br><input type="checkbox" name="check_list[]" value="Marketing "> Marketing<br><br><input type="checkbox" name="check_list[]" value="Web "> Web<br>
<section id="messageLabel" class="labelClass"><label>Relevant Experience:</label></section>
<section id="messageInput" class="inputClass"><textarea name="experience" id="experience" cols="30" rows="3"></textarea><br></section><br>
<section id="buttonClass" class="buttonClass"><input src="images/submit.png" onmouseover="this.src='images/submit2.png'" onmouseout="this.src='images/submit.png'" alt="submit" value="submit" height="25" type="image" width="70"></section>
</p>
</form>

我拥有的 PHP 脚本是:

<?php date_default_timezone_set('Europe/London');
$from = 'From: company@company.com'; 
$today = date('l, F jS Y.'); 
$to = 'secret@secret.com'; 
//Form Fields
$name = $_POST['name'];
$studentid = $_POST['studentid'];
$email = $_POST['email'];
$contactnumber = $_POST['contactnumber'];
$experience = $_POST['experience'];
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
        $check=$check.',';
}
}


//Admin Email Body
$subject = 'ClickTeesside.com Get Involved Request';
$bodyp1 = "You have received a Get Involved Request through the ClickTeesside website on $today \n\nFrom: $name ($studentid)\nEmail Address: $email\nContact Number: $contactnumber";
$bodyp2 = "\n\nInterests include: ".$check;
$bodyp3 = "\n\nPrevious Experience: $experience";
$bodyp4 = "\n\nIf suitable, please get in touch with this candidate as soon as possible - the candidate has also received an automated response.";
$body=$bodyp1.$bodyp2.$bodyp3.$bodyp4;
mail ($to, $subject, $body, $from);

//Candidate Email
$candidatesubject = 'ClickTeesside.com Get Involved: Automated Email';
$candidatefrom =  'From: ClickTeesside.com';
$cbody = "Thank you for emailing Click Radio about becoming involved with the station.\nWe've sent your details to our Station Management, who will review your application.\nIf successful, we will contact you in due course.\n\n";
$cbody2 = "Here is a copy of what you sent to us on $today:\n\nName: $name ($studentid)\nEmail Address: $email\nContact Number: $contactnumber\nSpecified Interested Areas: ".$check."\n\nPrevious Experience: $experience";
$candidatebody = $cbody.$cbody2;
mail ($email, $candidatesubject, $candidatebody, $from);

header("Location: http://www.google.co.uk/");
?>

看不到我哪里出错了 - 所以如果你能指出我正确的方向:) 谢谢!

4

3 回答 3

1

问题在这里:

foreach($_POST['check_list'] as $check) {
    $check=$check.',';
}

$check在循环的每次迭代中设置为当前项的值。在最后一个循环中,$check将设置为最后一项,然后您将追加,.

要解决此问题,请在循环中使用不同的变量名称:

$check = "";
foreach($_POST['check_list'] as $c) {
    $check .= $c.',';
}

上面保留了更多的代码并更好地说明了问题,但是做到这一点的方法是使用implode函数。

$check = implode(",", $_POST['check_list']);

这将为您提供没有尾随逗号的相同字符串。

于 2013-04-17T20:51:40.050 回答
0

编辑这个:

if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
    $check=$check.',';
}
}

有了这个:

$allChecks = '';
if(!empty($_POST['check_list'])) {
foreach($_POST['check_list'] as $check) {
    $allChecks .= $check.',';
}
}

然后通过邮件发送 $allChecks:

$bodyp2 = "\n\nInterests include: ".$allChecks;
于 2013-04-17T20:45:10.710 回答
0

您在 foreach 中所做的是每次都替换该值。使用 .= 将下一个值添加到您的字符串中。

其他一些调整是检查 PHP 和客户端中是否存在所有字段。如果我编写纯 PHP,我倾向于使用相同的文件,所以 if(isset($_POST)): 所以表单动作是在引用自己。

祝你好运。

于 2013-04-17T20:51:54.350 回答