Matthew Harrington
Matthew Harrington

Reputation: 13

Form checkboxes to PHP email - code not working so far

I have a basic form, with checkboxes, that on submission is to email to the administrator (and a copy to the client). The code I have echoes only the last checkbox (which therefore only displays one checkbox on the emails) - I can't get it to send all of the checkbox information in the email (even after trying several versions of code from on here).

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

The PHP Script that I have is:

<?php date_default_timezone_set('Europe/London');
$from = 'From: [email protected]'; 
$today = date('l, F jS Y.'); 
$to = '[email protected]'; 
//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/");
?>

Can't see where i am going wrong - so if you could point me in the right direction :) Thanks!

Upvotes: 0

Views: 206

Answers (3)

Ali Gangji
Ali Gangji

Reputation: 1503

The problem is here:

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

$check is set to the value of the current item on each iteration of the loop. On the last loop, $check will be set to the last item and then you will append ,.

To resolve the issue, use a different variable name in the loop:

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

The above preserves more of your code and better illustrates the problem, but the way to do this is with the implode function.

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

This will give you the same string without a trailing comma.

Upvotes: 1

James Cowie
James Cowie

Reputation: 464

What you are doing in your foreach is replacing the value each time. use .= to pre pend the next value to your string.

Some other tweaks would be to check all fields are present in both PHP and client side. I tend to use the same file if im writing pure PHP, so an if(isset($_POST)): so the form action is referencing itself.

Good luck.

Upvotes: 0

Alberto Fecchi
Alberto Fecchi

Reputation: 3396

Edit this:

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

with this:

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

then send $allChecks by mail:

$bodyp2 = "\n\nInterests include: ".$allChecks;

Upvotes: 0

Related Questions