PHP电子邮件的表单复选框 - 代码到目前为止无效

时间:2013-04-17 20:38:05

标签: php html5 email

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

<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/");
?>

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

3 个答案:

答案 0 :(得分: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']);

这将为您提供相同的字符串,不带尾随逗号。

答案 1 :(得分: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;

答案 2 :(得分:0)

你在foreach中所做的是每次都取代价值。使用。=预先将下一个值挂起到你的字符串。

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

祝你好运。