HTML Form Submission via PHP - php

Attempting to submit an HTML5 form to email via PHP, with a redirect to a "Thank you!" page after a successful submission. Problem is, the form isn't sending and the redirect isn't happening.
Here's my HTML:
<form id="vendorInfo" action="process_form_vendor.php" method="post">
<label for="vendorName">Vendor Name:</label>
<br />
<input id="vendorName" name="vendorName" type="text" maxlength="30" required>
<br />
<label for="contactName">Contact Name:</label> <br />
<input id="contactName" name="contactName" type="text" maxlength="35" required>
<br />
<label for="vendorType">Organization Type:</label>
<br />
<select id="vendorType" name="vendorType">
<option value="carrier">
Insurance Carrier
</option>
<option value="tech_crm">
Technology/CRM Management
</option>
<option value="leadProvider">
Lead Provider
</option>
<option value="info_comm">
Information/Communication
</option>
<option value="other">
Other (please describe below)
</option>
</select>
<br />
<label for="other1">Other Organization Type:</label>
<br />
<input id="other1" name="other1" type="text" maxlength="25">
<br />
<label for="email">Email:</label>
<br />
<input id="email" name="email" type="email" maxlength="30" required>
<br />
<label for="phone">Phone:</label>
<br />
<input id="phone" name="phone" type="tel" maxlength="12" required placeholder="xxx-xxx-xxxx">
<br />
<label for="questions">Any questions or comments? Leave them here:</label>
<br />
<textarea id="questions" name="questions" rows="10" maxlength="300"></textarea>
<br />
<br />
<fieldset id="selectionBox">
<legend id="packageSelect">
The following sponsorship packages are available for the Sales Summit; contact <ahref="mailto:email#domain.com”>Amanda</a> for pricing and details:
</legend>
<input type="radio" name="packageSelect" value="Bronze Package" checked> Bronze
<br />
<br />
<input type="radio" name="packageSelect" value="Silver Package"> Silver
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Lunch Package"> Gold (breakfast; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Breakfast Package"> Gold (lunch; exclusive sponsorship)
<br />
<br />
<input type="radio" name="packageSelect" value="Gold Trade Show Package"> Gold (trade show; exclusive sponsorship)
</fieldset>
<br />
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
<br />
</form>
And here is my PHP:
<?php
if(!isset($_POST['submit']))
{
echo "error; you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)||(empty($contactName)||(empty($vendorType)||(empty($email)||(empty($phone)||(empty($packageSelect)) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email#domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.\n".
"Vendor Type: $vendorType\n".
"Other Vendor Type: $other1\n".
"Email Address: $email\n".
"Phone Number: $phone\n".
"Additional Questions: $questions\n".
"Sponsorship Level: $packageSelect\n".
$to = 'email#domain.net';
$headers = "$email_from \r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
header('Location: thank-you.html');
?>
I have no idea what's going on or why this isn't working. Any ideas?

(Tested) Give this a try, it worked for me.
Plus, you may get an error saying "headers already sent", which did for me, so I used an echo at the end and I commented your header(".... to test with. If you have a space before <?php this could cause the error message to appear. You can try using ob_start(); just below your opening PHP tag.
Your empty conditionals ) had some too many, and some missing/not at the right spot.
Plus, a missing closing semi-colon at the end of "Sponsorship Level: $packageSelect\n". where there was a dot. Plus a missing From: which has been added.
<?php
// uncomment line below to use with header redirect
// ob_start();
if(!isset($_POST['submit']))
{
echo "error you need to submit the form!";
}
$vendorName = $_POST['vendorName'];
$contactName = $_POST['contactName'];
$vendorType = $_POST['vendorType'];
$other1 = $_POST['other1'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$questions = $_POST['questions'];
$packageSelect = $_POST['packageSelect'];
if (empty($vendorName)|| empty($contactName)|| empty($vendorType)|| empty($email)|| empty($phone)|| empty($packageSelect)){
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}
$email_from = 'email#domain.net';
$email_subject = '2014 SMS Sales Summit - New Vendor Reservation Request';
$email_body = "You have received a new vendor reservation request for the 2014 SMS Sales Summit from $contactName at $vendorName.\n".
"Vendor Type: $vendorType\n".
"Other Vendor Type: $other1\n".
"Email Address: $email\n".
"Phone Number: $phone\n".
"Additional Questions: $questions\n".
"Sponsorship Level: $packageSelect\n";
$to = "email#domain.net";
$headers = 'From: ' . $email_from . "\r\n";
$headers .= "Reply-To: $email \r\n";
mail($to,$email_subject,$email_body,$headers);
// header('Location: thank-you.html');
echo "thanks";
?>
Footnotes:
If it still does not "send", then change:
<button type="submit" name="submit">Submit</button>
to:
<input type="submit" name="submit" value="Submit">
If you use the header("... along with the ob_start(); you must not use the echo below it. Just comment that out.

You have syntax error in $email_from and $to. Need to use '(single quotes) or " (double quotes) instead of ‘ . Try this,
$email_from = 'email#domain.net';
...^ ^....
instead of
$email_from = ‘email#domain.net’;
Also, in your if condition you have missed to add lots of )
if (empty($vendorName)||
empty($contactName)||
empty($vendorType)||
empty($email)||
empty($phone)||
empty($packageSelect) ) {
echo "Vendor Name, Contact Name, Vendor Type, Email, Phone, and Package Selection are mandatory!";
exit;
}

<button type="submit&" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
to
<button type="submit" name="submit">Submit</button> <button type="reset" name="reset">Reset</button>
Is the form submitting from frontend?
also in php code, the $email_from:
$email_from = ‘email#domain.net’;
change it to:
$email_from = 'email#domain.net';
note the difference in single quotes. Same goes for $to:
$to = ‘email#domain.net';
change it to:
$to = 'email#domain.net';

Can you remove the "&" in <button type ="submit&">?
You also need to close the form using </form> after the last line

Why the & in type="submit&"? Also, there is no closing </form> tag.

you forgot to put a semicolon after $email_body
please put one
$email_body = "";

Related

How to get multi field form data in email body

Complete newbie at PHP. help.
I have written a large "join the club" form with 15 fields. the first 10 fields are required. last 5 are optional.
Each field has ID and names. name="fname" name="lname", for each field. (see below)___
<!-- FORM STARTS HERE -->
<form id="appliform" name="applform" method="post"
action="joinformscripts/test11.php"
enctype="text/plain" >
<!-- php code still in progress. test memb.php --->
<label for="fname">*First Name:</label><br>
<input type="text" id="fname" name="fname" required><br>
<label for="lname">*Last Name:</label><br>
<input type="text" id="lname" name="lname" required><br>
<label for="bizname">*Business Name:</label><br>
<input type="text" id="bizname" name="biz" required><br>
<label for="bizadd">*Business Mailing Address:</label><br>
<input type="text" id="bizadd" name="bizadd" required> <br>
<label for="city">*City:</label><br>
<input type="text" id="city" name="city" required> <br>
<label for="state">*State:</label><br>
<input type="text" id="state" name="state" required> <br>
<label for="zip">Zip:</label><br>
<input type="text" id="zip" name="zip" required> <br>
<label for="bizphone">*Business Phone:</label><br>
<input type="text" id="bizphone" name="bizphone" required> <br>
<label for="celphone">Personal Phone:</label><br>
<input type="text" id="celphone" name="celphone"> <br>
<label for="email">*Email:</label><br>
<input type="email" id="email" name="email" required><br>
<hr>
<h5>Complete this information if you are also paying for an Associate Membership. </h5>
<label for="fname">Associate First Name:</label><br>
<input type="text" id="afname" name="afname"><br>
<label for="lname">Associate Last Name:</label><br>
<input type="text" id="alname" name="alname"><br>
<label for="bizphone">Associate Business Phone:</label><br>
<input type="text" id="abizphone" name="abizphone" > <br>
<label for="celphone">Associate Personal Phone:</label><br>
<input type="text" id="acelphone" name="acelphone"> <br>
<label for="aemail">Associate Email:</label><br>
<input type="aemail" id="aemail" name="aemail"><br>
<hr>
<p>test 11 # 6.00p</p>
<!-- SEND BUTTON Submit using secure POST DATA button
when sent, opens remit payment page (in script)-->
<input type="submit" value="submit application" ><br>
<input type="reset" value="clear form">
<br>
<br>
<p>Please remit payment on the next page.</p>
</form> <!-- END MEMBERSHIP FORM with script action -->
PHP script.
<?php
// test 11
// after submit, goes to Remit page for payment: yes. //
// receive email with content in body: NO //
$email_to = "membership#xxxx.org";
$email_from = "membership#xxxx.org";
$reply_to = $_POST["email"];
$headers = "submitted by: .$mailfrom .$fname .$lname ";
$email_subject = "New Member Application Form (phptest8) revised 6.00pm thursday dec 10";
$headers = "From: " . $email_from . "\n";
$headers .= "Reply-To: " . $reply_to . "\n";
// NOT CAPTURING form variables.
//This needs to send all the fields in the form on Join page.
if(isset($_POST['submit'])){
$to = "membership#efwba.org"; // this is your Email address
$from = $_POST['email']; // this is the sender's Email address
$first_name = $_POST['fname'];
$last_name = $_POST['lname'];
$subject = "Membership Form submission";
$message = $first_name . " " . $last_name . "
wrote the following:" . "\n\n" . $_POST['message'];
$message2 = "my information " . $fname . "\n\n" . $fname . $lname . $biz . $bizadd . $city . $state . $zip . $bizphone . $celphone . $email . $afname . $alname . $abizphone . $acelphone. $aemail. $_POST['message'];
}
//Send the email. If the email is sent we will go to a REMIT PAYMENT page, if there is an error we will display a brief message.
ini_set("sendmail_from", $email_from);
$sent = mail($email_to, $email_subject, $email_body, $headers, $email_from);
if ($sent)
{
header("Location:/joinformscripts/thank-you-for-joining.html");
} else {
echo "There has been an error submitting your application. Please go back one page to verify all data has been entered correctly.";
}
// Function to validate against any email injection attempts
function IsInjected($str)
{
___
The host server -goDaddy, Cpanel- processes the php file and sends the email to the "Membership#" account.
After the form submits, it redirects to the Payment page, that part works!
However, I am NOT getting the field data inside the body of the email.
I see the starter line, but not what the applicant entered for contact info.
I want the body/ data to be a simple format, so I can copy it all and drop into a spreadsheet (for club mail merge printing)
It does not need to show the data labels.
I want to see this data in the email body:
"I want to join your organization!"
fname lname
biz
bizaddress
city state zip
bizphone
celphone
bizemail
QUESTION: what is the simplest method to capture each field into email_body?
Thank you!

I am trying to get my PHP and form working correctly

Hey I have a simple coding issue and I am hoping someone can spot the error of my ways.
I have a contact page http://mattmcdougall.ca/contact
this page has a form that is connected to a form PHP.
it is half working, I receive the email with ONLY the name subject and recipient (me). I do not get the other information in the form. (phone number and content do not get placed in the email) ALSO, I want the website to bring the person filling out the form to http://mattmcdougall.ca/contactdone currently it goes to a blank form.php page
here is the form code and PHP code, please feel free to clean up PHP as I used a template I found online.
<form action="form.php" method="post" class="formtext">
<input name="Name" type="text" value="enter name" maxlength="60"><br>
<input name="Email" value="enter email" type="text"><br>
<input name="Phone Number" value="enter phone#"type="text"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br><input name="Submit" type="submit" value="Submit"><form
and the PHP
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<TITLE>Matt McDougall Photography</TITLE>
<META NAME="Generator" CONTENT="EditPlus">
<META NAME="Author" CONTENT="">
<META NAME="Keywords" CONTENT="">
<META NAME="Description" CONTENT="">
</HEAD>
<BODY>
<?php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $Name . " <" . $email . ">\r\n";
mail($recipient, $subject, $mail_body, $phone, $header); //mail command :)
if(mail)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
?>
</BODY>
</HTML>
You're going to need to change the name of your form elements to match the params that you're expecting to receive via $_POST.
Right now you have (for example):
<input name="Phone Number" value="enter phone#"type="text"><br><br>
But you're going to need to name it:
<input name="phone" value="enter phone#"type="text"><br><br>
The name attribute is what is going to be submitted via the form to your PHP script. You need to use the same name on both sides.
Also what is the $phone variable in your mail function call? Those areas are reserved for additional headers and not just $phone. You'll need to concatenate the phone number into the $mail_body like so (with additional formatting of course):
$mail_body = $mail_body . " " . $phone;
This is the correct signature:
bool mail ( string $to , string $subject , string $message [, string $additional_headers [, string $additional_parameters ]] )
Edit: In addition to all of this, your $header is also wrong as it's trying to use the $Name variable which does not exist. The $name variable does exist.
I think this below should solve your problems.
Form:
<form action="form.php" method="post" class="formtext">
<input type="text" name="name" placeholder="Enter Name" maxlength="60"><br>
<input type="text" name="email" placeholder="Enter Email"><br>
<input type="text" name="phone" placeholder="Enter Phone #"><br><br>
<textarea name="interest" cols="60" rows="10" placeholder="Interest?"></textarea><br>
<input type="submit" name="submit" value="Submit">
</form>
form.php
<?php
$name = $_POST["name"];
$email = $_POST["email"];
$phone = $_POST["phone"];
$interest = $_POST["interest"];
$to = "matt#mattmcdougall.ca";
$subject = "Interested Client";
$headers = "From: {$name} <{$email}>\r\n";
$mail_body = <<<EMAIL
Name: {$name}
Phone: {$phone}
Message:
{$interest}
EMAIL;
$mail_success = mail($to, $subject, $mail_body, $headers);
if($mail_success) {
echo "http://mattmcdougall.ca/contactdone.html"; // are you trying to redirect to here?
} else {
echo "Sorry, something went wonky! Please try again!";
}
?>
HTTP POST variables are case sensitive, and the names of the input fields do not match that of those in PHP. <input type="text" name="datanamehere" /> with $_POST["datanamehere"]. Like what other answers said:
HTML:
<form action="form.php" method="post" class="formtext">
<input name="name" placeholder="Full name" type="text" maxlength="60"><br>
<input name="email" placeholder="Email" type="email"><br>
<input name="phone" placeholder="Phone number" type="tel"><br><br>
<textarea name="interest" placeholder="Interested in?" cols="60" rows="10"></textarea><br>
<input type="submit" value="Submit">
</form>
Notice I changed the value to placeholder, and used type="email" and type="tel".
form.php
$name = $_POST['name']; //senders name
$email = $_POST['email']; //senders e-mail adress
$phone = $_POST['phone'];
$recipient = "matt#mattmcdougall.ca"; //recipient
$mail_body = $_POST['interest']; //mail body
$subject = "interested client"; //subject
$header = "From: ". $name . " <" . $email . ">\r\n";
if (mail($recipient, $subject, $mail_body, $header)) //mail command :)
'http://mattmcdougall.ca/contactdone.html';
else
echo "Sorry Something Went Wonky, Please Try Again!";
As they said, mail() is a function returning a boolean. Wrap it around if to check for success.
Your element names do not match the ones in the PHP code and your form is not properly closed using .
<form action="form.php" method="post" class="formtext">
<input name="name" type="text" value="enter name" maxlength="60"><br>
<input name="email" type="text" value="enter email"><br>
<input name="phone" type="text" value="enter phone#"><br><br>
<textarea name="Interest" cols="60" rows="10">Interest?</textarea><br>
<input name="submit" type="submit" value="Submit">
</form>
mail is a function, so checking it in the 'if' by just writing mail isn't going to work.
try this instead
if(mail()):
// go to contactdone.html
header('Location: http://www.google.com');
exit();
else:
echo "Sorry Something Went Wonky, Please Try Again!";
endif;
Couple of things you can try,
if(mail)'http://mattmcdougall.ca/contactdone.html' is not going to do
anything, try using Header('Location
http://mattmcdougall.ca/contactdone.html')
Whenever you use $_POST the parameter name inside the [''] need to
match your input names (Example: <input name="interest" type="text">
would be $_POST['interest'])
If you want to show text inside an input use placeholder="enter
phone#" instead of value.

Adding checkboxes to PHP POST email form

I'm trying to build a form for wordpress. I've used plugins in the past but I need maximum control for some specific styling. I'm not very good with PHP yet, so am struggling trying to add checkboxes to the script.. I've removed my attempts and left the checkboxes in the html, but not in the PHP - can someone advise me of the best way to make any selected checkboxes visible in the email that is sent? Everything else works at the moment.
the html:
<form method="post" action="<?php bloginfo('template_directory'); ?>/contact.php">
<h3>Your Details</h3>
<div class="formrow">
<input type="text" name="Name" maxlength="99" id="fullname" placeholder="Name" />
<input type="email" name="Email" maxlength="99"placeholder="Email Address" />
<input type="tel" name="Phone" maxlength="25" placeholder="Phone Number" />
</div>
<h3>Project Type</h3>
<div class="formrow">
<fieldset>
<input type="checkbox" name="project1" value="Web">
<label for="type1">Web</label>
<input type="checkbox" name="project2" value="Digital">
<label for="type2">Digital</label>
<input type="checkbox" name="project3" value="Consultancy">
<label for="type3">Consultancy</label>
</fieldset>
</div>
<table align=center>
<tr><td colspan=2><strong>Contact us using this form:</strong></td></tr>
<tr><td>Department:</td><td><select name="sendto"> <option value="general#mycompany.com">General</option> <option value="support#mycompany.com">Support</option> <option value="sales#mycompany.com">Sales</option> </select></td></tr>
<tr><td>Company:</td><td><input size=25 name="Company"></td></tr>
<tr><td>Subscribe to<br> mailing list:</td><td><input type="radio" name="list" value="No"> No Thanks<br> <input type="radio" name="list" value="Yes" checked> Yes, keep me informed<br></td></tr>
<tr><td colspan=2>Message:</td></tr>
<tr><td colspan=2 align=center><textarea name="Message" rows=5 cols=35></textarea></td></tr>
<tr><td colspan=2 align=center><input type=submit name="send" value="Submit"></td></tr>
<tr><td colspan=2 align=center><small>A <font color=red>*</font> indicates a field is required</small></td></tr>
</table>
</form>
the PHP:
<?php
$to = $_REQUEST['sendto'] ;
$from = $_REQUEST['Email'] ;
$name = $_REQUEST['Name'] ;
$headers = "From: $from";
$subject = "Web Contact Data";
$fields = array();
$fields{"Name"} = "Name";
$fields{"Company"} = "Company";
$fields{"Email"} = "Email";
$fields{"Phone"} = "Phone";
$fields{"list"} = "Mailing List";
$fields{"Message"} = "Message";
$body = "We have received the following information:\n\n"; foreach($fields as $a => $b){ $body .= sprintf("%20s: %s\n",$b,$_REQUEST[$a]); }
$headers2 = "From: noreply#YourCompany.com";
$subject2 = "Thank you for contacting us";
$autoreply = "Thank you for contacting us. Somebody will get back to you as soon as possible, usualy within 48 hours. If you have any more questions, please consult our website at www.oursite.com";
if($from == '') {print "You have not entered an email, please go back and try again";}
else {
if($name == '') {print "You have not entered a name, please go back and try again";}
else {
$send = mail($to, $subject, $body, $headers);
$send2 = mail($from, $subject2, $autoreply, $headers2);
if($send)
{print "Success";}
else
{print "We encountered an error sending your mail, please notify webmaster#YourCompany.com"; }
}
}
?>
Thanks! MC
An array of checkboxes would be most appropriate. By using [] in the checkbox names, PHP will automatically parse them into a native array.
<input type="checkbox" name="projects[]" value="Web">
<label for="type1">Web</label>
<input type="checkbox" name="projects[]" value="Digital">
<label for="type2">Digital</label>
<input type="checkbox" name="projects[]" value="Consultancy">
<label for="type3">Consultancy</label>
On the PHP side:
$selectedProjects = 'None';
if(isset($_POST['projects']) && is_array($_POST['projects']) && count($_POST['projects']) > 0){
$selectedProjects = implode(', ', $_POST['projects']);
}
$body .= 'Selected Projects: ' . $selectedProjects;
Outputs (if all checked)
Selected Projects: Web, Digital, Consultancy

PHP contact form validation

I was just wondering if anybody could please help if this PHP code would work for validating HTML contact form inputs. I followed a tutorial to create this PHP validation, but i'm not sure if it will work. I don't have a webhost yet to test it out, but if anybody has a server,I will really be appreciated if anybody could do me a favour & try the code if you can send/receive email. Thank you!!
I'm using jQuery Validation Plugin for validating the form on client-side and this is the tutorial http://www.youtube.com/watch?v=rdsz9Ie6h7I
HTML form:
<form action="contact.php" method="post">
<label for="yourname">Your Name:</label>
<input type="text" name="YourName"/>
<label for="youremail">Your Email:</label>
<input type="text" name="YourEmail" />
<label for="yourmessage">Your Message:</label>
<textarea name="YourMessage"></textarea>
<fieldset>
<input type="submit" id="submit" value="Send"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
PHP Code:
<?php
/* Subject and Email Variables */
$emailSubject = 'Email from site visitor';
$webMaster = 'YourEmail#mail.com';
/* Getting Form Data Variables */
$nameField = $_POST['YourName'];
$emailField = $_POST['YourEmail'];
$messageField = $_POST['YourMessage'];
$body = <<<EOD
<br><hr><br>
Name: $YourName <br>
Email: $YourEmail <br>
Message: $YourMessage <br>
EOD;
$headers = "From: $YourEmail\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<html>
<head>
</head>
<body>
<p style="font-size:12px;font-family:Tahoma,Verdana;">Thanks for your Message.</p>
</body>
</html>
EOD;
echo "$theResults";
?>
If you don't have a webserver right now you can install a local one and test it there. Completly free.
http://www.apachefriends.org/en/xampp-windows.html#641
/* Getting Form Data Variables */
$nameField = $_POST['YourName'];
$emailField = $_POST['YourEmail'];
$messageField = $_POST['YourMessage'];
$body = <<<EOD
<br><hr><br>
Name: $nameField<br>
Email: $emailField <br>
Message: $messageField <br>
Lots of issues
You're not validating at all on the PHP side. I added validation and conditional rendering of success or failure messages below
You're not using the retrieved variable names (instead using $YourEmail) - the post names
Your form labels don't match the input names
Your form element isn't closed, and your fields aren't all in the fieldset, and the fieldset has no legend.
On case of failure, I added a value prefill in the form, so people don't get returned to a blank form when it falls to submit.
To do that, I put the form at the end, after the PHP
Code:
(Note: This would all be put into the file contact.php)
<?php
if (isset($_POST['submit'])) {
/* Getting Form Data Variables */
$nameField = isset($_POST['YourName']) ? $_POST['YourName'] : null;
$emailField = isset($_POST['YourEmail']) ? $_POST['YourEmail'] : null;
$messageField = isset($_POST['YourMessage']) ? $_POST['YourMessage'] : null;
// Validate
$failures = array();
if (strlen($nameField)) $failures[] = "Name is required";
if (strlen($emailField)) $failures[] = "Email is required";
if (filter_var($email,FILTER_VALIDATE_EMAIL) === false) $failures[] = "Email is invalid";
if (strlen($messageField)) $failures[] = "Message is required";
// If validation errors, render them
if (count($failures)) {
echo "<p><b>Failed to submit: " . implode(", ", $failures) . "</b></p>";
} else {
/* Subject and Email Variables */
$emailSubject = 'Email from site visitor';
$webMaster = 'YourEmail#mail.com';
$body = <<<EOD
<br><hr><br>
Name: {$nameField} <br>
Email: {$emailField} <br>
Message: {$messageField} <br>
EOD;
$headers = "From: {$emailField}\r\n";
$headers .= "Content-type: text/html\r\n";
$success = mail($webMaster, $emailSubject, $body, $headers);
$theResults = <<<EOD
<p>Thanks for your Message.</p>
EOD;
echo "$theResults";
}
}
?>
<form action="contact.php" method="post">
<fieldset>
<legend>Contact Us</legend>
<label for="YourName">Your Name:</label>
<input type="text" name="YourName" value="<?=$nameField?>" />
<label for="YourEmail">Your Email:</label>
<input type="text" name="YourEmail" value="<?=$emailField?>" />
<label for="YourMessage">Your Message:</label>
<textarea name="YourMessage"><?=$messageField?></textarea>
<input type="submit" id="submit" value="Send"/>
<input type="reset" id="reset" value="Reset"/>
</fieldset>
</form>

php tick box array for email form

i'm new to stackoverflow but after trying a number of way i can't get my head around this email form. The form works fine but i've tried to add 4 optional tick boxes so the client can tick so we know which package they're interested in. I think i've got the front end setup properly:
<p><label for="author">Full Name:</label><br />
<input type="text" name="cf_name" class="details" tabindex="1" /></p>
<p><label for="telephone">Telephone:</label><br />
<input type="text" name="cf_telephone" class="details" tabindex="2" /></p>
<p><label for="email">E-mail:</label><br />
<input type="text" name="cf_email" class="details" tabindex="3" /></p>
<p><label for="package">Package Type:</label><br />
<input type="checkbox" name="cf_package[]" class="package" value="Business English Classes" tabindex="4" /> Business English Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="English Conversation Classes" tabindex="4" /> English Conversation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Exam Preparation Classes" tabindex="4" /> Exam Preparation Classes<br />
<input type="checkbox" name="cf_package[]" class="package" value="Writing Skills Classes" tabindex="4" /> Writing Skills Classes
</p>
<p><label for="message">Questions/Comments:</label><br />
<textarea type="text" name="cf_message" class="questions" tabindex="8"></textarea></p>
<p style="padding-top:10px;"><input type="submit" name="submit" value="Submit" class="button" tabindex="9" /> <input type="reset" name="submit" value="Clear" class="button" tabindex="10" /></p>
My backend script is as follows:
<code><?php
// get all values from form and remove spaces before/after values
$field_name = trim($_POST['cf_name']);
$field_telephone = trim($_POST['cf_telephone']);
$field_email = trim($_POST['cf_email']);
$field_package = trim($_POST['cf_package']);
$field_message = trim($_POST['cf_message']);
$mail_to = 'theenglishbeehive#gmail.com';
$subject = 'Enquiry from '.$field_name;
// check if user input own e-mail -> generate headers and send mail
if ($field_email && $field_message)
{
// generate body of message
$body_message = "From: ".$field_name."\n";
$body_message .= "Telephone: ".$field_telephone."\n";
$body_message .= "E-mail: ".$field_email."\n";
$body_message .= "Package Type: ".$field_package."\n";
$body_message .= "Questions/Comments: ".$field_message;
$headers = "From: ".$field_email."\n";
$headers .= "Reply-To: ".$field_email."\n";
$headers .= "Return-Path: ".$field_email."\n";
$headers .= "X-Priority: 3 (Normal)\n";
// send email
$mail_status = mail($mail_to, $subject, $body_message, $headers);
}
// success ?
if ($mail_status)
{
?>
<script language="javascript" type="text/javascript">
window.location = 'enquiry-sent.php';
</script>
<?php
}
else
{
?>
<script language="javascript" type="text/javascript">
alert('Message failed. Please, send an email to theenglishbeehive#gmail.com');
window.location = 'index.php';
</script>
<?php
}
?>
</code>
Hopefully i've got this showing properly on the website too.
The selected 'package' will be sent to the server as an array, not as a string, because you're using the array notation ([]) and using checkboxes, people can select multiple packages.
You might want to convert the array to a comma-separated string and show which packageS the user selected:
$field_package = implode(', ', $_POST['cf_package']);
and then:
$body_message .= "Package Type(s): ".$field_package."\n";
If only a single option should be allowed to be selected, you might consider using a radio-button in stead of checkboxes.

Categories