Posting multiple checkboxes using PHP and sending to an email - php

I am fairly new with using PHP. I have a form that I am using to send a contact form form a webpage to an email address.
I am having trouble passing multiple checkboxes styled with a comma. For example, if the user picks checkbox1 and checkbox4, the code will be styled: checkbox1, , , checkbox4. I don't want to display the commas, unless the user picks the checkboxes. Let me know if that makes sense or not.
This is the HTML:
<code>
<form id="contact_form" class="contact" method="post" action="email_process.php">
<input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
<input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>
<input class="email text" name="email" type="email" placeholder="EMAIL:" required>
<input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>
<label>Would you like to be contacted by:</label>
<input name="how" class="emailbtn radio" type="checkbox" value="Email">
<label>EMAIL</label>
<input name="how2" class="phonebtn radio" type="checkbox" value="Phone">
<label>PHONE NUMBER</label><br><br>
<div class="one">
<label class="margin">EVENT DATE:</label><br>
<input name="month" class="month small" type="number" placeholder="MM">
<input name="day" class="day small" type="number" placeholder="DD">
<input name="year" class="year small" type="number" placeholder="YYYY">
</div>
<div class="one">
<label class="margin">EVENT LOCATION:</label><br>
<input name="location" class="location text" type="text">
</div>
<label>Services we may assist you with:</label><br><br>
<div class="two">
<input name="services" class="chefbtn radio" type="checkbox" value="Personal Chef">
<label>PERSONAL CHEF</label><br>
<input name="services2" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
<label>PRIVATE COOKING CLASSES</label>
</div>
<div class="two">
<input name="services3" class="chefbtn radio" type="checkbox" value="Event Catering">
<label>EVENT CATERING</label><br>
<input name="services4" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
<label>RESTERAUNT CONSULTING</label>
</div>
<input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
<textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>
<input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
</form>
</code>
This is my PHP:
<code>
<?php
//Prefedined Variables
$to = "Enter email here";
// Email Subject
$subject = "Contact from your website.";
// This IF condition is for improving security and Prevent Direct Access to the Mail Script.
if($_POST) {
// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = stripslashes($_POST['how']);
$how2 = stripslashes($_POST['how2']);
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = stripslashes($_POST['services']);
$services2 = stripslashes($_POST['services2']);
$services3 = stripslashes($_POST['services3']);
$services4 = stripslashes($_POST['services4']);
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);
if ( ! empty($how) && ! empty($how2) ) {
$contact_type = $how.', '.$how2;
} elseif (! empty($how)){
$contact_type = $how;
} elseif (! empty($how2)){
$contact_type = $how2;
}
if ( ! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) {
$contact_type2 = $services. ', ' .$services2. ', ' .$services3. ', ' .$services4;
}
// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$contact_type.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$contact_type2.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';
// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';
if (! empty($how) || ! empty($how2)) {
if (! empty($services) || ! empty($services2) || ! empty($services3) || ! empty($services4)) {
if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($heard) && ! empty($comments) ) {
// Sending Email
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
}
}
</code>
EDIT:
After some research and the right guidance I came across an answer that helped me solve this. If anyone else is having the same problem feel free to refer to this.
<?php
//Prefedined Variables
$to = "Enter email here";
// Email Subject
$subject = "Contact from your website.";
// This IF condition is for improving security and Prevent Direct Access to the Mail Script.
if($_POST) {
// Collect POST data from form
$firstname = stripslashes($_POST['firstname']);
$lastname = stripslashes($_POST['lastname']);
$email = stripslashes($_POST['email']);
$how = 'None';
if(isset($_POST['how']) && is_array($_POST['how']) && count($_POST['how']) > 0){
$selectedHow = implode(', ', $_POST['how']);
}
$phone = stripslashes($_POST['phone']);
$month = stripslashes($_POST['month']);
$day = stripslashes($_POST['day']);
$year = stripslashes($_POST['year']);
$location = stripslashes($_POST['location']);
$services = 'None';
if(isset($_POST['services']) && is_array($_POST['services']) && count($_POST['services']) > 0){
$selectedServices = implode(', ', $_POST['services']);
}
$heard = stripslashes($_POST['heard']);
$comments = stripslashes($_POST['comments']);
// Collecting all content in HTML Table
$content='<table width="100%">
<tr><td align "center"><b>Contact Details</b></td></tr>
<tr><td>First Name:</td><td> '.$firstname.'</td></tr>
<tr><td>Last Name:</td><td> '.$lastname.'</td></tr>
<tr><td>Email:</td><td> '.$email.' </td></tr>
<tr><td>Phone:</td> <td> '.$phone.'</td></tr>
<tr><td>How Should We Contact You?</td> <td> '.$selectedHow.'</td></tr>
<tr><td>Event Date:</td><td> '.$month.' / '.$day.' / '.$year.' </td></tr>
<tr><td>Location:</td> <td> '.$location.'</td></tr>
<tr><td>Which Services Are You Interested In?</td> <td> '.$selectedServices.'</td></tr>
<tr><td>How Did You Hear About Us?</td> <td> '.$heard.'</td></tr>
<tr><td>Comments:</td> <td> '.$comments.'</td></tr>
<tr><td>Date:</td> <td> '.date('d/m/Y').'</td></tr>
</table> ';
// Define email variables
$headers = "From:".$email."\r\n";
$headers .= "Reply-to:".$email."\r\n";
$headers .= 'Content-type: text/html; charset=UTF-8';
if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone) && ! empty($selectedHow) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($selectedServices) && ! empty($heard) && ! empty($comments) ) {
// Sending Email
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "Please go back and make sure that all fields have been filled out.";
return false;
}
}
else {
print "An error occured and your message could not be sent.";
return false;
}
}
<form id="contact_form" class="contact" method="post" action="email_process.php">
<input class="firstname text" name="firstname" type="text" placeholder="FIRST NAME:" required>
<input class="lastname text" name="lastname" type="text" placeholder="LAST NAME:" required><br><br>
<input class="email text" name="email" type="email" placeholder="EMAIL:" required>
<input class="name text" name="phone" type="tel" placeholder="PHONE NUMBER:" required><br><br>
<label>Would you like to be contacted by:</label>
<input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
<label>EMAIL</label>
<input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
<label>PHONE NUMBER</label><br><br>
<div class="one">
<label class="margin">EVENT DATE:</label><br>
<input name="month" class="month small" type="number" placeholder="MM">
<input name="day" class="day small" type="number" placeholder="DD">
<input name="year" class="year small" type="number" placeholder="YYYY">
</div>
<div class="one">
<label class="margin">EVENT LOCATION:</label><br>
<input name="location" class="location text" type="text">
</div>
<label>Services we may assist you with:</label><br><br>
<div class="two">
<input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
<label>PERSONAL CHEF</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
<label>PRIVATE COOKING CLASSES</label>
</div>
<div class="two">
<input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
<label>EVENT CATERING</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
<label>RESTERAUNT CONSULTING</label>
</div>
<input name="heard" class="heard text" type="text" placeholder="HOW DID YOU HEAR ABOUT US?">
<textarea name="comments" class="comments" type="text" placeholder="ADDITIONAL COMMENTS:"></textarea>
<input class="submit" type="image" src="../images/contact/s1_submit_btn.png">
</form>

You need to have the name of those checkboxes as an array. Like:
<label>Would you like to be contacted by:</label>
<input name="how[]" class="emailbtn radio" type="checkbox" value="Email">
<label>EMAIL</label>
<input name="how[]" class="phonebtn radio" type="checkbox" value="Phone">
<label>PHONE NUMBER</label><br><br>
Do same for services:
<input name="services[]" class="chefbtn radio" type="checkbox" value="Personal Chef">
<label>PERSONAL CHEF</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Private Cooking Classes">
<label>PRIVATE COOKING CLASSES</label>
</div>
<div class="two">
<input name="services[]" class="chefbtn radio" type="checkbox" value="Event Catering">
<label>EVENT CATERING</label><br>
<input name="services[]" class="cateringbtn radio" type="checkbox" value="Resteraunt Consulting">
Then in your php:
$how = stripslashes($_POST['how']);
// is actually an array and holds its values in a comma separated format
//no need for how2
//The same approach for services: it's also an array now. if you followed the html above
$services = stripslashes($_POST['services']);
It will also be good if you assign different unique error messages unlike you did along
if( ! empty($firstname) && ! empty($lastname) && ! empty($email) && ! empty($phone) && ! empty($how) && ! empty($month) && ! empty($day) && ! empty($year) && ! empty($location) && ! empty($services) && ! empty($heard) && ! empty($comments) ) {
// Sending Email
if( mail($to, $subject, $content, $headers) ) {
print "Thank you, I will get back to you shortly!<br>";
return true;
}
else {
print "An error occured and your message could not be sent.";//here 1
return false;
}
}
else {
print "An error occured and your message could not be sent.";//here 2
return false;
}
}
You can then find out if it's the outer if( ! empty($firstname) && ... or the mail function that is failing

try it with php method array filter and implode:
<?php
$services[] = "test";
$services[] = "";
$services[] = "test";
$services[] = "";
$arr = array_filter($services, function($elem){
return $elem != "";
});
echo implode(", ", $arr);

Related

After submiting a php form I get empty window and no e-mail

Update:
I've added echo $errors to the end of my code. Now I am getting that all fields are required although all are filled:
} else {
echo $errors;
}
I've created a simple php e-mail form with this example (link). And jQuery form validator (link).
Now what I get after submiting a form is just a empty "contact-form-handler.php" page.
You can see the form live at: mantasmilka.com/#contactPage
What could be the problem? Below is my code.
contact-form-handler.php
<?php
$errors = '';
$myemail = 'mantas#mantasmilka.com';//<-----Put Your email address here.
if(empty($_POST['infoname']) ||
empty($_POST['infocompany']) ||
empty($_POST['infoemail']) ||
empty($_POST['infophone']) ||
empty($_POST['infodescription'])
)
{
$errors .= "\n Error: all fields are required";
}
$jobtype = $_POST['jobtype'];
$budget = $_POST['budget'];
$location = $_POST['location'];
$infoname = $_POST['infoname'];
$infocompany = $_POST['infocompany'];
$infoemail = $_POST['infoemail'];
$infophone = $_POST['infophone'];
if (isset($_POST['infodescription'])) {
$infodescription = $_POST['infodescription'];
}
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$infoemail))
{
$errors .= "\n Error: Invalid email address";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "Contact form submission: $name";
$email_body = "You have received a new message. ".
" Here are the details:\n Jobtype: $jobtype \n Budget: $budget \n Location: $location \n Name: $infoname \n Company: $infocompany \n E-mail: $infoemail \n Phone: $infophone \n ".
"Message \n $infodescription";
$headers = "From: $myemail\n";
$headers .= "Reply-To: $infoemail";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
// header('Location: http://www.mantasmilka.com/index.php');
header('Location: index.php');
exit();
} else {
echo $errors;
}
?>
my form in index.php:
<form id="contactform" method="post" name="contact_form" action="contact-form-handler.php">
<div class="left">
<fieldset class="jobtype">
<legend>Job type</legend>
<input type="radio" name="jobtype" id="project" value="project" required>
<label for="project">Project</label>
<input type="radio" name="jobtype" id="part-time" value="part-time" >
<label for="part-time">Part-time</label>
<input type="radio" name="jobtype" id="full-time" value="full-time" >
<label for="full-time">Full-time</label>
</fieldset>
<fieldset class="budget">
<legend>Budget</legend>
<input type="radio" name="budget" id="budget5k" value="budget5k" required>
<label for="budget5k">5k € ></label>
<input type="radio" name="budget" id="budget10k" value="budget10k" >
<label for="budget10k">5k - 10k €</label>
<input type="radio" name="budget" id="budget15k" value="budget15k" >
<label for="budget15k">15k € <</label>
</fieldset>
<fieldset class="location">
<legend>Location</legend>
<input type="radio" name="location" id="locremote" value="locremote" required>
<label for="locremote">Remote</label>
<input type="radio" name="location" id="loclocal" value="loclocal" >
<label for="loclocal">Local with relocation</label>
</fieldset>
</div>
<div class="right">
<fieldset class="contactinfo">
<legend>Your Contact Info</legend>
<div class="input-holder">
<input type="text" name="infoname" id="infoname" value="" required data-validation="alphanumeric">
<label for="infoname">Name</label>
</div>
<div class="input-holder">
<input type="text" name="infocompany" id="infocompany" value="" required data-validation="alphanumeric">
<label for="infocompany">Company</label>
</div>
<div class="input-holder">
<input type="text" name="infoemail" id="infoemail" value="" required data-validation="email">
<label for="infoemail">E-mail</label>
</div>
<div class="input-holder">
<input type="text" name="infophone" id="infophone" value="" required data-validation="number">
<label for="infophone">Phone</label>
</div>
<div class="input-holder textarea">
<textarea name="infodescription" form="contact_form" rows="4" required></textarea>
<label for="infodescription">Message</label>
</div>
<div class="input-holder submit">
<input type="submit" name="submit" value="Submit" required/>
</div>
</fieldset>
</div>
</form>
text field should look like this don't put form tag in here
<textarea name="infodescription" rows="4" required></textarea>
in php make the if statement like this the first ( opens the if statement the last ) closes it and take not of how i used the other brackets
if(
(empty($_POST['infoname'])) ||
(empty($_POST['infocompany'])) ||
(empty($_POST['infoemail'])) ||
(empty($_POST['infophone'])) ||
(empty($_POST['infodescription']))
)
{
$errors .= "\n Error: all fields are required";
}
The efficient way to solve the problem is:
$('#contactform').submit(function(ev) {
ev.preventDefault();
if($('#infodescription').val() == '')
{
alert('Description is required');
return false;
}
//add other validation
this.submit(); // If all the validations succeeded
});
By this you can avoid the unwanted server load.

Can't get php contact form to work

I have a webpage that has two contact forms and the second one is working fine but I can't seem to get the first one to work. I am fairly new to php. Thanks in advance for the help. Here's the html:
<h3>Message Us</h3>
<form action="?" method="POST" onsubmit="return saveScrollPositions(this);">
<input type="hidden" name="scrollx" id="scrollx" value="0" />
<input type="hidden" name="scrolly" id="scrolly" value="0" />
<div id="msgbox1">
<label for="name2">Name:</label>
<input type="text" id="name2" name="name2" placeholder=" Required" />
<label for="email2">Email:</label>
<input type="text" id="email2" name="email2" placeholder=" Required" />
<label for="phone">Phone:</label>
<input type="text" id="phone" name="phone" placeholder="" />
<label for= "topic">Topic:</label>
<select id="topic" name="topic">
<option value="general">General</option>
<option value="stud">Property Price Enquiry</option>
<option value="sale">Bull Sale Enquiry</option>
</select>
</div><!--- msg box 1 -->
<div id="msgbox2">
<textarea id="message" name="message" rows="7" colums="25" placeholder="Your Message?"></textarea>
<p id="feedback2"><?php echo $feedback2; ?></p>
<input type="submit" value="Send Message" />
</div><!--- msg box 2 -->
</form><!--- end form -->
</div><!--- end message box -->
And here's the php:
<?php
$to2 = '418#hotmail.com'; $subject2 = 'MPG Website Enquiry';
$name2 = $_POST ['name2']; $email2 = $_POST ['email2']; $phone = $_POST ['phone']; $topic = $_POST ['topic']; $message = $_POST ['message'];
$body2 = <<<EMAIL
This is a message from $name2 Topic: $topic
Message: $message
From: $name2 Email: $email2 Phone: $phone
EMAIL;
$header2 = ' From: $email2';
if($_POST['submit']=='Send Message'){
if($name2 == '' || $email2 == '' || $message == ''){
$feedback2 = '*Please fill out all the fields';
}else {
mail($to2, $subject2, $body2, $header2);
$feedback2 = 'Thanks for the message. <br /> We will contact you soon!';
} } ?>
For the saveScrollPositions I use the following php and script:
<?php
$scrollx = 0;
$scrolly = 0;
if(!empty($_REQUEST['scrollx'])) {
$scrollx = $_REQUEST['scrollx'];
}
if(!empty($_REQUEST['scrolly'])) {
$scrolly = $_REQUEST['scrolly'];
}
?>
<script type="text/javascript">
window.scrollTo(<?php echo "$scrollx" ?>, <?php echo "$scrolly" ?>);
</script>
You have a mistake in your HTML part i.e.
<input type="submit" value="Send Message" />
add attribute name also in this input type like this-
<input type="submit" value="Send Message" name="submit" />
one thing more to correct in your php script is write-
$header2 = "From: ".$email2; instead of $header2 = ' From: $email2';
now try it.
What do you return in "saveScrollPositions" function? if you return false the form submit wont fire.

form processing a radio button in php

I have an rsvp form that I am trying to send the value of a radio button but unsure of the php for it, my code is as follows:
<div class="form-group">
<input type="text" name="name" id="name" size="30" value="" placeholder="Name(s)" class="text-input form-control" />
<label class="error" for="name" id="name_error">Name is required.</label>
</div>
<div class="form-group">
<input type="radio" name="response" id="response_accepted" value="accepted" checked="checked" > Will be there
<input type="radio" name="response" id="response_declined" value="declined"> Won't make it
<label class="error" for="response" id="name_error">Please select an option</label>
</div>
<div class="form-group">
<input type="text" name="guests" id="guests" size="30" value="" placeholder="No. of guests attending" class="text-input form-control" />
<label class="error" for="guests" id="guests_error">No. of guests is required.</label>
</div>
but i'm not sure of the php that collects the value of the radio button but have got the other fields to pass the data using:
<?php
if ((isset($_POST['name'])) && (strlen(trim($_POST['name'])) > 0)) {
$name = stripslashes(strip_tags($_POST['name']));
} else {$name = 'No name entered';}
if ((isset($_POST['guests'])) && (strlen(trim($_POST['guests'])) > 0)) {
$guests = stripslashes(strip_tags($_POST['guests']));
} else {$guests = 'No # of guests entered';}
?>
It will process in the same way as the other, the only difference is that you already know its value, so you will need to
if ((isset($_POST['response'])) && ($_POST['response'] == 'accepted' ) {
$response = 'Accepted';
} else {$response= 'Not Accepted';}
This code will help you
$response = $_POST['response'] ;
if(isset($response) && $resonse == "accepted" )){
echo "Accepted" ;
}
else{ echo "Declined" ; }

PHP form send to specific email by Select option

The contact form I am currently working on sends to 3 email addresses. I would like to have the form send to another email address only when the Naples location option is selected. The other locations do not currently need a separate email address. I tried adding in
switch($_POST['location'])
{
case “naples″: $my_email = ‘naples#email.com’; break;
}
I thought this had worked but no email was sent out when submitted and I couldn't distinguish an error. Is it possible to allow one location option when selected to tack on another email address to the original 3 recipients? Thank you guys in advance code posted below.
<?
ob_start();
if(isset($_POST['registersubmit']))
{
unset($badcaptcha);
require_once('captcha.php');
$privatekey = "....";
$resp = recaptcha_check_answer ($privatekey,$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
if (!$resp->is_valid)
{
$badcaptcha=true;
}
else
{
//Send email
//foo#alpha.com
$to = "foo#alpha.com\r\n";
$from = "foo#alpha.com <foo#alpha.com>";
$subject = "Contact Foo\r\n";
$body = "Contact Foo\r\n\n";
$body .= "Full Name: ".$_POST['Fname']."\r\n\n";
$body .= "Business Name: ".$_POST['Bname']."\r\n\n";
$body .= "Email: ".$_POST['email']."\r\n\n";
$body .= "Address: ".$_POST['address']."\r\n\n";
$body .= "City: ".$_POST['city']."\r\n\n";
$body .= "State: ".$_POST['state']."\r\n\n";
$body .= "Zip Code: ".$_POST['zipcode']."\r\n\n";
$body .= "Phone: ".$_POST['phone']."\r\n\n";
$body .= "Location: ".$_POST['location']."\r\n\n";
$body .= "Interests: \r\n\n";
$body .= "*".$_POST['services1']."*".$_POST['services2']."*".$_POST['services3']."*".$_POST['services4']."*".$_POST['services5']."*".$_POST['services6']."*".$_POST['services7']."*".$_POST['services8']."*".$_POST['services9']."\r\n\n";
$body .= "Comments/Message: ".$_POST['details']."\r\n\n";
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/plain; charset=iso-8859-1\r\n";
mail($to, $subject, $body, "From: ".$from."\r\n".$headers);
//Send copy beta#foo.com
$to = "beta#foo.com\r\n";
mail($to, $subject, $body, "From: ".$from."\r\n".$headers);
$to = "tom#foo.com\r\n";
mail($to, $subject, $body, "From: ".$from."\r\n".$headers);
header("Location:thankyou_contact2.php");
}
}
$linkname="contact";
if(empty($id)) { $id = 10; $showplus = "Y"; }
include("includes/header.php");
?>
<div id="main"><table width="621" cellspacing="0" cellpadding="0">
<h1>CONTACT US</h1>
<br />
Use Our Online Form (below) or Contact Your Nearest Location
<?
if(isset($badcaptcha))
{
?>
<b><font color="red">INCORRECT SECURITY CODE, PLEASE TRY AGAIN</font></b><br /> <br />
<?
}
?>
<br />
<br />
<div id="contact_form"> <!--onsubmit="return validate2()" -->
<form method="post" name="contact_form" onsubmit="return validate3()" >
<label>
<span>Full Name<font color="#FF0000">*</font></span>
<input size="31" name="Fname" value="<? if(!empty($_POST['Fname'])) { echo $_POST['Fname']; } else { echo "Full Name"; } ?>" class="input-text" maxlength="100" OnFocus="if (this.value == 'Full Name') {this.value=''}" onblur="if (this.value == '') {this.value='Full Name'}"/>
</label>
<label>
<span>Business Name<font color="#FF0000">*</font></span>
<input size="31" name="Bname" value="<? if(!empty($_POST['Bname'])) { echo $_POST['Bname']; } else { echo "Business Name"; } ?>" class="input-text" maxlength="100" OnFocus="if (this.value == 'Business Name') {this.value=''}" onblur="if (this.value == '') {this.value='Business Name'}"/>
</label>
<label>
<span>Email Address<font color="#FF0000">*</font></span>
<input size="31" name="email" value="<? if(!empty($_POST['email'])) { echo $_POST['email']; } else { echo "Email Address"; } ?>" class="input-text" maxlength="100" onfocus="if (this.value == 'Email Address') {this.value=''}" onblur="if (this.value == '') {this.value='Email Address'}" />
</label>
<label>
<span>Address</span>
<input size="31" name="address" value="<? if(!empty($_POST['address'])) { echo $_POST['address']; } else { echo "Address"; } ?>" class="input-text" maxlength="100" OnFocus="if (this.value == 'Address') {this.value=''}" onblur="if (this.value == '') {this.value='Address'}" />
</label>
<label>
<span>City</span>
<input size="31" name="city" value="<? if(!empty($_POST['city'])) { echo $_POST['city']; } else { echo "City"; } ?>" class="input-text" maxlength="100" OnFocus="if (this.value == 'City') {this.value=''}" onblur="if (this.value == '') {this.value='City'}" />
</label>
<label> <span>State</span><select name="state" id="state">
<option value="State">State</option>
<option value="AK">Alaska</option>
<option value="AL">Alabama</option>
</select> </label>
<label>
<span>Zip Code</span>
<input size="31" name="zipcode" value="<? if(!empty($_POST['zipcode'])) { echo $_POST['zipcode']; } else { echo "Zip Code"; } ?>" class="input-text" maxlength="10" OnFocus="if (this.value == 'Zip Code') {this.value=''}" onblur="if (this.value == '') {this.value='Zip Code'}" />
</label>
<label>
<span>Phone<font color="#FF0000">*</font></span>
<input size="31" name="phone" value="<? if(!empty($_POST['phone'])) { echo $_POST['phone']; } else { echo "Phone"; } ?>" class="input-text" maxlength="30" OnFocus="if (this.value == 'Phone') {this.value=''}" onblur="if (this.value == '') {this.value='Phone'}" />
</label>
<label> <span>Location<font color="#FF0000">*</font></span> <select name="location" id="location">
<option value="location">Nearest Location</option>
<option value="Naples">Naples</option>
<option value="Cherokee">Cherokee</option>
</select> </label>
<label>
<span> </span>
<strong>Interests</strong> <br />
<input type="checkbox" name="services1" value="Trucks Sales"<? if($_POST['services1']=="Trucks Sales") { echo " checked"; } ?> /> Trucks Sales
<input type="checkbox" name="services2" value="Rentals & Leasing"<? if($_POST['services2']=="Rentals & Leasing") { echo " checked"; } ?> /> Rentals & Leasing
</label>
<!-- <label>
<span> </span>
<input type="checkbox" name="services2" value="Rentals & Leasing"< ? if($_POST['services2']=="Rentals & Leasing") { echo " checked"; } ?> /> Rentals & Leasing
</label> -->
<label>
<span> </span>
<input type="checkbox" name="services3" value="Parts"<? if($_POST['services3']=="Parts") { echo " checked"; } ?> /> Parts
<input type="checkbox" name="services4" value="Service & Repair"<? if($_POST['services4']=="Service & Repair") { echo " checked"; } ?> /> Service & Repair
</label>
<!-- <label>
<span> </span>
<input type="checkbox" name="services4" value="Service & Repair"< ? if($_POST['services4']=="Service & Repair") { echo " checked"; } ?> /> Service & Repair
</label> -->
<label>
<span> </span>
<input type="checkbox" name="services5" value="Fleet Maintenance Management"<? if($_POST['services5']=="Fleet Maintenance Management") { echo " checked"; } ?> /> Fleet Maintenance Management
<input type="checkbox" name="services6" value="Other"<? if($_POST['services6']=="Other") { echo " checked"; } ?> /> Other
</label>
<!-- <label>
<span> </span>
<input type="checkbox" name="services6" value="Other"< ? if($_POST['services6']=="Other") { echo " checked"; } ?> /> Other
</label> -->
<label>
<span> </span>
<strong>Message/Comments</strong>
<br />
<textarea cols="80" rows="8" name="details"><? echo $_POST['details']; ?></textarea></label>
<label>
<span> </span>
<script type="text/javascript">
var RecaptchaOptions = { theme : 'clean' };
</script>
<?php
require_once('captch.php');
$publickey = "..."; // you got this from the signup page
echo recaptcha_get_html($publickey);
?>
</label>
<label>
<span> </span>
<input type="submit" name="registersubmit" value="Submit" /></label>
</form></div>
<? includecontent($id); ?>
</td>
<!-- new --></tr>
</table>
</td>
</tr>
<tr>
<td width="581" align="right" valign="top" colspan="2"><img src="pictures/spacer.gif" border="0" width="10" height="10"></td>
</tr>
</table>
</td>
</tr>
</table><br />
<img src="pictures/spacer.gif" border="0" width="3" height="3" /></td>
</tr>
</table>
</div><!-- main -->
</div><!-- content -->
<?
include("includes/footer.php");
?>
You should be setting the $to variable, not $my_email.

Form not picking up first name

I am having trouble figuring our exactly why my php code won't pick up the first name from my form, I hope you guys can help.
Everything else is emailed through fine, just the first name field which is element_1_1.
Here is the PHP:
$<?php
$errors = '';
$myemail = 'example#example.co.uk';
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
if (!preg_match(
"/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i",
$email_address))
{
$errors .= "\n Error: Invalid email address \n";
}
if( empty($errors))
{
$to = $myemail;
$email_subject = "";
$email_body = "".
"\n First Name: $name \n \n Second Name: $lastname \n \n Email: $email_address \n \n Phone Number: $number \n \n Area(s): $area \n \n Discount: $discount \n".
$headers = "From: example#example.co.uk\n";
$headers .= "Reply-To: $email_address";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: http://www.example.co.uk');
}
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Contact form handler</title>
</head>
<body>
<!-- This page is displayed only if there is some error -->
<?php
echo nl2br($errors);
echo "";
echo " \n <a href='http://www.example.com'>Go Back</a> \n";
?>
</body>
</html>
And here is the form:
<div id="bmvform-form_container">
<form id="form_507993" class="appnitro" method="post" action="formsend.php">
<div class="form_description">
<h2>Want To Receive Our Properties?</h2>
<p><em>At Pure Acquisitions, we respect your privacy. Any details provided will NOT be sent to any third party without your consent.</em></p>
</div>
<ul >
<li id="li_1" >
<label class="description" for="element_1">Name </label>
<span>
<input id="element_1_1" name="element_1_1" class="element text" maxlength="255" size="14" value=""/>
<label>First</label>
</span>
<span>
<input id="element_1_2" name="element_1_2" class="element text" maxlength="255" size="14" value=""/>
<label>Last</label>
</span><p class="guidelines" id="guide_1"><small>We don't want to be rude!</small></p>
</li> <li id="li_3" >
<label class="description" for="element_3">Email Address </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_3"><small>To send you important stuff!</small></p>
</li> <li id="li_2" >
<label class="description" for="element_2">Phone Number </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_2"><small>We need to let you know about our properties!</small></p>
</li> <li id="li_5" >
<label class="description" for="element_5">Locations You Are Interested In </label>
<span>
<input id="element_5_1" name="element_5" class="element radio" type="radio" value="All" />
<label class="choice" for="element_5_1">All</label>
<input id="element_5_2" name="element_5" class="element radio" type="radio" value="London" />
<label class="choice" for="element_5_2">London</label>
<input id="element_5_3" name="element_5" class="element radio" type="radio" value="South of England" />
<label class="choice" for="element_5_3">South of England</label>
<input id="element_5_4" name="element_5" class="element radio" type="radio" value="West Midlands" />
<label class="choice" for="element_5_4">West Midlands</label>
<input id="element_5_5" name="element_5" class="element radio" type="radio" value="East Midlands" />
<label class="choice" for="element_5_5">East Midlands</label>
<input id="element_5_6" name="element_5" class="element radio" type="radio" value="North West" />
<label class="choice" for="element_5_6">North West</label>
<input id="element_5_7" name="element_5" class="element radio" type="radio" value="North East" />
<label class="choice" for="element_5_7">North East</label>
<input id="element_5_8" name="element_5" class="element radio" type="radio" value="Yorkshire & Humber" />
<label class="choice" for="element_5_8">Yorkshire & Humber</label>
<input id="element_5_9" name="element_5" class="element radio" type="radio" value="Wales" />
<label class="choice" for="element_5_9">Wales</label>
<input id="element_5_10" name="element_5" class="element radio" type="radio" value="Scotland" />
<label class="choice" for="element_5_10">Scotland</label>
</span><p class="guidelines" id="guide_5"><small>Let us know where in the country you are looking for property.</small></p>
</li> <li id="li_4" >
<label class="description" for="element_4">Ideal Discount From Market Value </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/>
</div><p class="guidelines" id="guide_4"><small>How much discount (in percent) are you looking for?</small></p>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="507993" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
</div>
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
You're missing the curley brackets here? I would assume this or something similar with curley brackets, depending on what you'd like to do.
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
{
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
}
What are you trying to do with the if-statement anyway? You're checking if one of them is empty and then set them all? Or you should check if one of them is empty and then send a message back to user that one of the fields needs to be filled? Then you should change it a bit to something like this:
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
{
do something; }
else {
$name = $_POST['element_1_1'];
$lastname = $_POST['element_1_2'];
$email_address = $_POST['element_3'];
$number = $_POST['element_2'];
$area = $_POST['element_5'];
$discount = $_POST['element_4'];
}
what is your if statement doing?
if( empty($_POST['element_1_1']) ||
empty($_POST['element_1_2']) ||
empty($_POST['element_3']) ||
empty($_POST['element_2']) ||
empty($_POST['element_5']) ||
empty($_POST['element_4']))
This seems to be unrelated to anything, but if none of them are empty, then $name is not set (It is the first line after the if statement, and will only be called if the if statement is true)
I would assume you were meant to execute all the lines, which requires curly braces around them.

Categories