OK I have a simple mail to where I am trying to email all my members based on a simple query.
Problem is I must be missing something due to the form just reloads and dose not send anything.
email.php
<form method="post" action="massemail.php">
Subject:</br>
<input type="text" name="subject" /><br />
Message:</br>
<textarea name="details"></textarea><br />
<input type="submit" value="Send"/>
</form>
massemail.php
require 'core/init.php';
require_once "email.php";
$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);
$results = mysql_query('SELECT email from users');
while(list($email) = mysql_fetch_row($results))
{
mail($email, '$subject', '$message');
}
have tried
<?php
require 'core/init.php';
require_once "email.php";
$message = "test";
$subject = "123";
if ($results = mysql_query('SELECT email from users')) {
while(list($email) = mysql_fetch_row($results)) {
mail($email, '$subject', '$message');
}
?>
First, you need to extract the email addresses from each row instead of using the row itself. That can easily be achieved with $row[0] in your case since your query has always one column, though you should probably try to use mysqli instead of the deprecated mysql.
Second, I would recommend calling mail() once only, to send one message to all people rather than one email per person! Why do you want to send the same email again and again like this? It is possible to use a comma-separated list of emails, as seen on the PHP: mail - Manual.
If you want to use mysql, your code could look something like this with PHP Arrays and the handy implode function:
require 'core/init.php'; // calls mysql_connect(...)
require_once "email.php";
$message = check_input($_POST['details']);
$subject = check_input($_POST['subject']);
if (mysql_ping()) { echo 'Ping worked'; } else { echo 'Ping failed'; } // DEBUG
$results = mysql_query('SELECT email from users');
$emails = array();
while($row = mysql_fetch_row($results))
{
array_push($emails, $row[0]);
}
echo implode(", ", $emails); // DEBUG
if (!empty($emails)) {
mail(implode(", ", $emails), $subject, $message);
}
It seems to me (while chatting with you) that the whole mechanism of HTML Forms + PHP (GET and/or POST) is not clear to you. You should probably read about all this a bit more, and hopefully the code below helps you:
<?php
// Put requires here
?>
<html>
<head>
<title>Test</title>
</head>
<body>
<h1>Demo form methods</h1>
<?php
if ((isset($_POST['subject']) && isset($_POST['body'])) || (isset($_GET['subject']) && isset($_GET['body']))) {
// Your code for sending an email could be here
?>
<h2>RESULTs</h2>
<ul>
<li>POST
<ul>
<li>suject: <?php echo $_POST['subject']; ?></li>
<li>suject: <?php echo $_POST['body']; ?></li>
</ul>
</li>
<li>GET
<ul>
<li>suject: <?php echo $_GET['subject']; ?></li>
<li>suject: <?php echo $_GET['body']; ?></li>
</ul>
</li>
</ul>
<?php
// The code above is just to show you how the variables in GET/POST work
} else {
// Below are two formulaires, you can keep one only of course!
?>
<h2>FORMs</h2>
<h3>GET method</h3>
<form method="get" action="email.php">
Subject: <input name="subject" type="text"><br>
Body: <input name="body" type="text"><br>
<input type="submit" value="Send with GET">
</form>
<h3>POST method</h3>
<form method="post" action="email.php">
Subject: <input name="subject" type="text"><br>
Body: <input name="body" type="text"><br>
<input type="submit" value="Send with POST">
</form>
<?php } ?>
</body>
</html>
It's not sending the email because you are not parsing the query results properly.
//connect to MySQL using following line. This is the NEW and better way of doing it.
$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
if ($result = $mysqli->query('SELECT email from users')) {
while($row = $result->fetch_object()){
mail($row->email, $subject, $message);
}
}
$result->close();
Related
I am creating e-learning that users will take a quiz. Upon submit they will be sent to the results page where it will show them their scores. If the user passes a link will pop up to a certificate they can print out. I cannot get the certificate to populate variables(first name, last name, etc).
Here is what I have:
index.php (home page where they take the quiz and fill out a form)
<form id="form1" name="form1" method="post" action="results.php" onsubmit="return validateForm() ;">
<label>Last Name:
<input type="text" name="lname" id="lname" tabindex="1" />
</label>
<label>First Name:
<input type="text" name="fname" id="fname" tabindex="2" />
</label>
<label>Title:
<input type="text" name="title" id="title" tabindex="3" /><br/><br/>
</label>
<?php
$quiz = new Quiz( $questions );
echo $quiz->renderQuiz();
?>
<input name="Submit" type="submit" value="Submit" />
</form>
On results.php (page that shows results and link to certificate)
<?php
$ip = $_SERVER['REMOTE_ADDR']; // employee's Ip address
$time = date("d/m/y : H:i:s", time()); // current timestamp
$email_string = create_email_string( $force_email, $_POST['email_to'] );
$questions_correct = array();
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'];
// Grades the quiz
$score = (string)round( ( count($questions_correct) / count($questions)*100), 2 );
$variables = array();
$variables['fname'] = $_POST['fname'];
$variables['lname'] = $_POST['lname'];
$variables['test_name'] = $test_name;
$variables['score'] = $score;
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Test Results</title>
<link rel="stylesheet" type="text/css" href="./stylesheets/quiz_stylesheet.css" />
</head>
<body>
<div id="white">
<div id="header">
<img src="./images/ucdheader.jpg" width="1000" id="header" /img>
</div>
<div id="mainContent">
<h1><span>You Scored <?php echo $score ?>%</span>
<br><?php if ($score >= 80)
echo "<a href=certificate.php >Print Certificate</a>
"?></h1>
<p>Below are your results for <?php echo $test_name; ?>. In order to pass this quiz, you must score 80% or better. If you would like to try again, click the following link.</p>
<a href='<?php echo $test_page_name ?>'>Try again</a>
<p>These results were sent to the following addresses: </p>
<ul>
<?php
$all_emailed_to = explode( "; " , $email_string);
foreach ($all_emailed_to as $email) {
echo "<li>" . $email . "</li>";
}
?>
</ul>
</div>
</div>
</body>
</html>
On certificate.php
<title>Certificate</title>
</head>
<body>
<div id="white">
<div class="btn-group btn-group-lg">
<a role="button" class="btn btn-default" href="#" data- function="print">Print Certificate</a>
</div>
<div class="certificate">
<img src="images/cert.png" width="820" height="527" id="cert" />
<div class="name">
<?php $variables['fname'] . " " . $variables['lname'] . ?>
</div>
<div class="course">
<p></p>
</div>
<div class="completion_date">
<?php $variables['time']; ?>
</div>
</div>
</div>
</body>
</html>
Thank you!
You are not passing any variables to the certificate.php page. There are several options of passing data between php pages such as:
1) Sessions
2) $_POST
3) $_GET
The easiest option in your code is using the $_GET variable. Simply use this line of code in results.php:
echo "<a href=certificate.php?fname={$variables['fname']}&lname={$variables['lname']} >Print Certificate</a>"?></h1>
Than in certificate.php retrieve the data by using:
<?php echo $_GET['fname'] . " " . $_GET['lname'] . ?>
Note 1: you will need to urlendode() the variables in the link in case someone puts special chairs in their name/last name
Note 2: It's a hackable approach. An employee could print a certificate with just any name/last name by calling a prepared url.
The best approach would be to rewrite all three pages to use Sessions: http://php.net/manual/en/features.sessions.php
Either you go for Kamran's approach and build a hidden form which will send by POST the information needed in the certificate page, either you go for a database oriented model(i.e. save users and their results and then retrieve those information to generate the certificate).
The first way is pretty unsafe since you can change the values of the fields using Firebug and send dirty unexpected input if you don't test the values.
In results.php store necessary values in SESSION:
$info_to_save = array( $_POST['lname'], $_POST['fname'], $_POST['title'], time);
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$_SESSION['certInfo'] = $info_to_save;
Then use it in certificate.php like this:
//Start session if not started
if (!isset($_SESSION)) {
session_start();
}
$lname = $_SESSION['certInfo'][0];
$fname = $_SESSION['certInfo'][1];
$title = $_SESSION['certInfo'][2];
$time = $_SESSION['certInfo'][3];
In my website, it presents a list of program titles. When a title is clicked, it displays the content and an email form. The form just takes an email and mails the title as the subject and the content of the page in the email.
The link will pass a variable 'info'. 'info' contains the ID for the post in my database. The problem occurs when I click the submit button. It will not send an email, and refresh the page. This causes the url to loose the 'info' variable and loose all content on the page.
The page works perfectly if I hardcode the ID in the php and don't use $_GET['info'].
Is there something I am missing?
<?php
$id = $_GET['info'];
/*****************************************************
open conection to the mySQL database
******************************************************/
$configs = include('config.php');
//Create a connection
$con = mysqli_connect(
$configs['host'], //host
$configs['username'], //username
$configs['password'], //password
$configs['dbname'] //dbname
);
if (!$con)
{
die('Could not connect: ' . mysqli_error($con));
}
/*****************************************************
Populate the page
******************************************************/
$sql="
SELECT p.post_title, p.post_content
FROM they_posts AS p
WHERE p.ID='".$id."'
";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result))
{
/*Title*/
echo '<h1>'.$row['post_title'].'</h1><hr>';
/*content*/
echo '<h2>Details: </h2><br>'.$row['post_content'].'<br>';
$title= $row['post_title'];
$content = $row['post_content'];
/*****************************************************
E-mail Form
******************************************************/
include('includes/email_test.php');
}
?>
And this is the email_test.php
<div data-role="collapsible">
<h1>Send Reminder Email</h1>
<?php
function spamcheck($field)
{
// Sanitize e-mail address
$field=filter_var($field, FILTER_SANITIZE_EMAIL);
// Validate e-mail address
if(filter_var($field, FILTER_VALIDATE_EMAIL))
{
return TRUE;
}
else
{
return FALSE;
}
}
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Your Email: <input type="text" name="to"><br>
<input type="submit" name="submit" value="Submit Feedback">
</form>
<?php
}
else // the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["to"]))
{
// Check if "from" email address is valid
$receivecheck = spamcheck($_POST["to"]);
if ($receivecheck==FALSE)
{
echo "Invalid input";
}
else
{
$to = $_POST["to"]; //receiver
$subject = $title;
$message = $content;
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("$to",$subject,$message,"From: noreply#address.com\n");
echo "reminder has been sent";
}
}
}
?>
</div>
I have used isset($id) to display a back button for when submit is pressed. This will bring back the information but the email is still never sent.
In your scenario you must have info=post_id in your current url to get $_GET['info']
1st way:
Change your form action like this:
<form method="post" action="<?php echo $_SERVER["PHP_SELF"].'?info='.$_GET['info']; ?>">
then in action it will be :
/your_page.php?info=current_post_id
then in action page you can get info by $_GET['info']
2nd way:
or you can add extra hidden form field in your form for post_id like this:
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
Your Email: <input type="text" name="to"><br>
<input type="submit" name="submit" value="Submit Feedback">
<input type="hidden" name="post_id" value="<?php echo $_GET['info'];">
</form>
After that In your action page you can get post_id by $_POST['post_id']
It should make sense!
Thanks
$_GET['info'] will only work if your form is defined with method='GET'.
If your form is defined with method='POST' then you need to use $_POST['info'].
If you want your code to work no matter whether the form is using GET or POST, then use $_REQUEST['info'].
I created a little form validator with PHP and having some problems with it.
MY VIEW FILE is here :
<form action="" method="post">
<?php if( isset($status) ) : ?>
<p class="notice"><?php echo $status; ?> </p>
<?php endif; ?>
<ul>
<li>
<label for="name">Your Name : </label>
<input type="text" name="name">
</li>
<li>
<label for="email">Your Email : </label>
<input type="text" name="email">
</li>
<li>
<input type="submit" value="Sign Up">
</li>
</ul>
</form>
and here's my little controller :
<?php
require 'index.tmpl.php';
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
?>
Now what happens is that , when I open up the page and leave the form fields blank and submit it ,it just reloads ,does not echo anything.
You want to echo $status, not $name.
How about moving the require line to below the if?
<?php
if ($_SERVER['REQUEST_METHOD'] == "POST") {
$name = trim($_POST['name']);
$email = trim($_POST['email']);
if (empty($name) || empty($email)) {
$status = "Please provide a name and a valid email address";
}
echo $name;
}
require 'index.tmpl.php';
?>
The <form action="" points to the location where the form will be submitted. If blank, it submits the form back to itself.
<form action="yourLittleController.php" method="POST">
Edited with more info:
in php, post is not POST. Example here: https://eval.in/89002
make sure you have
method="POST">
and not
method="POST">
you should mention your second file name in your view file's form's action attribute like action = "controller.php"
I have two .php files in the same folder on my computer. The first file is called "Client Instructions.php" and the second file is called "form_data_checker.php".
In the "Client Instructions.php" file, I have this snippet of code:
$required = array('name', 'comments');
require 'form_data_checker.php';
In the "form_data_checker.php" file, I have this code:
if(empty($temp) && in_array($key, $required)){ // etc.}
I'm using DreamWeaver and it looks like my "require" statement is correctly combining the two files. However, when I run my program I get error messages that the $required variable is not recognized as an array.
Can someone help me figure out why my $required array is not being recognized in my code in the "form_data_checker.php" file?
Here is the exact error message I'm getting:
Notice: Undefined variable: expected in C:\xampp\htdocs\introducingphp\form_data_checker.php on line 16
Warning: in_array() expects parameter 2 to be array, null given in C:\xampp\htdocs\introducingphp\form_data_checker.php on line 16
Thanks!
Ok, I'm including the entire contents of the two files because I'd really like to get this solved. Here is "Client Instructions.php":
<?php
require './includes/form_data_checker.php';
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Client Contact Information</title>
</head>
<body>
<h1>Client Information</h1>
<?php if ($myErrors || $somethingsMissing) { ?>
<p class="warning"> Please fix the item(s) indicated. </p>
<?php } ?>
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="name"> Name:
<?php if ($somethingsMissing && in_array('name', $somethingsMissing)) { ?>
<span class="warning"> Please enter your name </span>
<?php } ?>
</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for"email"> Email (optional):
<?php if ($somethingsMissing && in_array('email', $somethingsMissing)) { ?>
<span class="warning"> Please enter your email </span>
<?php } ?>
</label>
<input type="email" name="email" id="email">
<p>
<label for="address"> Address:
<?php if ($somethingsMissing && in_array('address', $somethingsMissing)) { ?>
<span class="warning"> Please enter your address </span>
<?php } ?>
</label>
<input type="text" name="address" id="address">
</p>
<p>
<input type="submit" name="send" id="send" value="Submit Information">
</p>
<form>
<pre>
<?php
if($_GET){
echo 'Contents of the $_GET array: <br>';
print_r($_GET);
} elseif ($_POST) {
echo 'Contents of the $_POST array: <br>';
print_r($_POST);
}
?>
</pre>
</body>
</html>
and here is "form_data_checker.php":
<?php
foreach ($_POST as $formFieldKeyName => $clientEnteredDataItem){
$tempClientData = is_array($clientEnteredDataItem) ? $clientEnteredDataItem : trim($clientEnteredDataItem);
if(empty($tempClientData) && in_array($formFieldKeyName, $requiredInfo)){
$somethingsMissing[]=$formFieldKeyName;
$$formFieldKeyName='';
} else if(in_array($formFieldKeyName, $expectedInfo)){
$$formFieldKeyName = $tempClientData;
}
}
?>
The problem is that
require './includes/form_data_checker.php';
is the first line, which is called before you actually declare $requiredInfo, so to ensure that the file is called after the array is declared, I moved the require statement. I think this should solve the issue.
<?php
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
require './includes/form_data_checker.php';
?>
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>Client Contact Information</title>
</head>
<body>
<h1>Client Information</h1>
<?php if ($myErrors || $somethingsMissing) { ?>
<p class="warning"> Please fix the item(s) indicated. </p>
<?php } ?>
<form name="contact" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<label for="name"> Name:
<?php if ($somethingsMissing && in_array('name', $somethingsMissing)) { ?>
<span class="warning"> Please enter your name </span>
<?php } ?>
</label>
<input type="text" name="name" id="name">
</p>
<p>
<label for"email"> Email (optional):
<?php if ($somethingsMissing && in_array('email', $somethingsMissing)) { ?>
<span class="warning"> Please enter your email </span>
<?php } ?>
</label>
<input type="email" name="email" id="email">
<p>
<label for="address"> Address:
<?php if ($somethingsMissing && in_array('address', $somethingsMissing)) { ?>
<span class="warning"> Please enter your address </span>
<?php } ?>
</label>
<input type="text" name="address" id="address">
</p>
<p>
<input type="submit" name="send" id="send" value="Submit Information">
</p>
<form>
<pre>
<?php
if($_GET){
echo 'Contents of the $_GET array: <br>';
print_r($_GET);
} elseif ($_POST) {
echo 'Contents of the $_POST array: <br>';
print_r($_POST);
}
?>
</pre>
</body>
</html>
Try to use global:
global $required;
if(empty($temp) && in_array($key, $required)){ // etc.}
its not necessary to use globals. it should work even without it.
$_GLOBALS['required'] = array('name', 'comments');
require 'form_data_checker.php';
//form_data_checker.php
if(empty($temp) && in_array($key, $_GLOBALS['required'])){ // etc.}
The above method can also work. However, it's not good practice. Use $_SESSION[] variables instead.
The problem is that you are including the "form_data_checker.php" file before you are defining $requiredInfo. Since $requiredInfo is not defined yet it results in the warning that parameter 2 is not an array.
Update "Client Instructions.php" so that "form_data_checker.php" is executed after you define and populate the $requiredInfo array.
"Client Instructions.php" should look like this after the change:
<?php
$myErrors = array();
$somethingsMissing = array();
$expectedInfo = array();
$requiredInfo = array();
if (isset($_POST['send'])){
$to = 'test#test.com';
$subject = 'Feedback from Client Information form';
$expectedInfo = array('name', 'email', 'comments');
$requiredInfo = array('name', 'comments');
}
// now when 'form_data_checker.php' is called, $requiredInfo will be defined
require './includes/form_data_checker.php';
?>
<!DOCTYPE HTML>
<html>
<head>
....
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: 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/");
?>
Can't see where i am going wrong - so if you could point me in the right direction :)
Thanks!
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.
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;
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.