Form not picking up first name - php

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.

Related

PHP form isn't working

Can anyone see what's wrong with these two files? When I click submit, it just redirects me to a blank page and I get no feedback from the form what so ever let alone an email.
HTML code
<form action="../script/form14.php" method="POST" >
<h1>KittyBoo</h1>
<p>Beställ kattmat online - alltid laktos- och glutenfri</p><fieldset class="formular">
<legend>1. Välj produkt</legend>
<ul>
<li>
<input type="checkbox" name="Kitty Extra Small" value="Kitty Extra Small"> <li> Kitty Extra Small: 90 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Small" value="Kitty Small"> <li> Kitty Small: 140 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Medium" value="Kitty Medium"> <li> Kitty Medium: 180 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Large" value="Kitty Large"> <li> Kitty Large: 210 kr
<br></li>
<li>
<input type="checkbox" name="Kitty Extra Large" value="Kitty Extra Large"> <li> Kitty Extra Large: 240 kr<br></li>
</ul>
</fieldset>
<fieldset class="formular">
<legend>2. Dina uppgifter</legend>
<label for="namn"> För- och efternamn:</label><br>
<input type="text" id="namn" name="from" required><br>
<label for="email">Epost:</label><br>
<input type="text" id="email" name="email" required><br>
<label for="emailconfirm">Konfirmera Epost:</label>
<input type="text" id="emailconfirm" name="emailconfirm" required>
<label for="adress">Adress:</label>
<input type="text" id="adress" name="adress" required>
<label for="postnummer">Postnummer:</label>
<input type="text" id="postnummer" name="postnummer" required>
<label for="stad">Stad:</label>
<input type="text" id="stad" name="stad" required>
<label for="land">Land:</label>
<select id="land" name="Land">
<option> Sverige</option>
<option> Finland</option>
<option> Norge</option>
<option> Danmark</option>
<option> Island</option>
</select>
</fieldset>
<fieldset class="formular">
<legend>3. Leverans</legend>
<label for="date">Välj datum för leverans:</label>
<input type="date" id="date" name="date" required/>
<p>Leveranssätt:</p>
<ul>
<li>
<input type="radio" name="radAnswer" required value="Posten"> <a> Posten</a>
<input type="radio" name="radAnswer" required value="DHL"> <a> DHL</a>
<input type="radio" name="radAnswer" required value="FedEx"> <a> FedEx</a>
</li>
</ul>
<p>Välj färg på din KittyBoo:</p>
<input type="radio" name="radAnswer" value="bla"> <a> Blå</a>
<input type="radio" name="radAnswer" value="rosa"> <a> Rosa</a>
<p>Övriga önskemål kring din leverans:</p>
<textarea name="subject" id="ovrig" rows="3" cols="30" maxlength="300" required> Max 300 ord</textarea>
</fieldset>
<input type="submit" value="skicka">
</form>
PHP Script
<?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;
}
}
?>
<?php
// display form if user has not clicked submit
if (!isset($_POST["submit"]))
{
?>
<?php
}
else
// the user has submitted the form
{
// Check if the "from" input field is filled out
if (isset($_POST["email"]))
{
// Check if "from" email address is valid
$mailcheck = spamcheck($_POST["email"]);
if ($mailcheck==FALSE)
{
echo "Invalid input";
}
else
{
$from = $_POST["from"]; // sender
$subject = $_POST["subject"];
$email = $_POST["email"];
function build_message($request_input){if(!isset($message_output))
{$message_output ="";}if(!is_array($request_input))
{$message_output = $request_input;}
else{foreach($request_input as $key => $value)
{if(!empty($value)){if(!is_numeric($key))
{$message_output .= str_replace("_"," ",ucfirst($key)).": ".build_message($value).PHP_EOL.PHP_EOL;}
else{$message_output .= build_message($value).", ";}}}}return rtrim($message_output,", ");}
$message = build_message($_REQUEST);
// message lines should not exceed 70 characters (PHP rule), so wrap it
$message = wordwrap($message, 70);
// send mail
mail("helenaevaschroder#gmail.com",$subject,$message, "From: $from\n");
echo "Tillbakaack för ditt meddelande";
echo "<p><a href=http://www.sh.se>Tillbaka till sidan</a></p>";
}
}
}
?>
The server is set up for PHP.
If it would be of any help, you can check the form out here:http://student.mtstudent.se/~sh14hf2407/pages/formular.html
Your PHP condition if (!isset($_POST["submit"])) { ... } is always FALSE because $_POST['submit'] doesn't exist in your html form. You have created an element <input type="submit" value="skicka"> but $_POST['submit'] means "an element which name attribute is submit". So replace :
<input type="submit" value="skicka">
by
<input type="submit" name="submit" value="skicka">
and this issue will be fixed.
change
<input type="submit" value="skicka">
into
<input type="submit" name="submit" value="skicka">

Posting multiple checkboxes using PHP and sending to an email

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

Issue with PHP Email script

I have a HTML form within one of my webpages, and i am now having trouble configuring it to the sendmail.php. When I fill in the form, and click submit, its asking me to save the file 'sendmail.php' but I want it to send to the email specified in the 'sendmail.php' file.
in my 'sendmail.php' file I currently have:
<?php
$element_1_1 = $_POST['element_1_1'];
$element_1_2 = $_POST['element_1_2'];
if(isset($_POST['submit'])){
$to = "sales#atechdigital.co.uk"; // this is your Email address
$from = $_POST['element_4']; // this is the sender's Email address
$first_name = $_POST['element_1_1'];
$last_name = $_POST['element_1_2'];
$subject = "Form submission";
$message = $element_1_1 . " " . $element_1_2 . " Has Sent A Quote Request";
$element_2 = $_POST['element_2'];
$element_3 = $_POST['element_3'];
$element_4 = $_POST['element_4'];
$element_5_1 = $_POST['element_5_1'];
$element_5_2 = $_POST['element_5_2'];
$element_5_3 = $_POST['element_5_3'];
$element_5_4 = $_POST['element_5_4'];
$element_5_5 = $_POST['element_5_5'];
$element_5_6 = $_POST['element_5_6'];
mail ($to,$element_4,$element_2,$element_3,$element_4,$element_5,$element_5_1,$element_5_2,$element_5_3,$element_5_4,$element_5_5,$element_5_6);
echo " Thank you " . $first_name . ", One of our sales team is dealing with you're request! We will contact you shortly.";
}
?>
and the html form is as follows:
<form id="form_824601" class="appnitro" method="post" action="sendmail.php">
<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="8" 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>
</li>
<li id="li_2" >
<label class="description" for="element_2">Company Name </label>
<div>
<input id="element_2" name="element_2" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_3" >
<label class="description" for="element_3">Enter Contact Telephone Number </label>
<div>
<input id="element_3" name="element_3" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_4" >
<label class="description" for="element_4">Enter Email Address </label>
<div>
<input id="element_4" name="element_4" class="element text medium" type="text" maxlength="255" value=""/>
</div>
</li>
<li id="li_5" >
<label class="description" for="element_5">Request A Quote For ... </label>
<span>
<input id="element_5_1" name="element_5" class="element radio" type="radio" value="1" />
<label class="choice" for="element_5_1">Web Design</label>
<input id="element_5_2" name="element_5" class="element radio" type="radio" value="2" />
<label class="choice" for="element_5_2">SEO</label>
<input id="element_5_3" name="element_5" class="element radio" type="radio" value="3" />
<label class="choice" for="element_5_3">Social Media</label>
<input id="element_5_4" name="element_5" class="element radio" type="radio" value="4" />
<label class="choice" for="element_5_4">Graphic Design</label>
<input id="element_5_5" name="element_5" class="element radio" type="radio" value="5" />
<label class="choice" for="element_5_5">Video Marketing</label>
<input id="element_5_6" name="element_5" class="element radio" type="radio" value="6" />
<label class="choice" for="element_5_6">Maintenance</label>
</span>
</li>
<li class="buttons">
<input type="hidden" name="form_id" value="824601" />
<input id="saveForm" class="button_text" type="submit" name="submit" value="Submit" />
</li>
</ul>
</form>
I Hope someone can shed light on the issue I'm having!!
This will never work while you are running the script locally. You need to run it on a server that can handle PHP. The only way you could run it locally and that is if you have a local web server set up.
I hope this helps!
You can't run .php files just from the folder, you will need a web server to handle it for you. if you are running windows or mac , you can set one up using software like XAMPP (win + osx) , WAMP (win) , MAMP (osx).
If you happen you run a linux distribution, you can set up a LAMP server.
Here are some links:
https://www.apachefriends.org/index.html
http://www.wampserver.com/en/
http://www.mamp.info/en/
From the links above, xampp has always worked out the best for me performance wise. parsing a huge xml feed and putting it into a mysql database takes like half the time on xampp compared to wamp.

PHP form sendeail.php not identifying new form items

I am trying to add new items to a legacy sendeail.php form. However, when I add the new items to the php listing and the mail delivery, they aren't displaying at all in the email that is sent. At least the emails are going through... I know that I need to refine the radio and check box items, but NONE of the new items are showing. New items are all items after $attn in the code list immediately below. Any and all help is appreciated, thank you very much!
My PHP code is:
<?php
$ip = $_POST['ip'];
$httpref = $_POST['httpref'];
$httpagent = $_POST['httpagent'];
$visitor = $_POST['visitor'];
$visitormail = $_POST['visitormail'];
$notes = $_POST['notes'];
$attn = $_POST['attn'];
$phone = $_POST['phone'];
$interest = $_POST['interest'];
$budget = $_POST['budget'];
$budget = $_POST['day'];
$timeslot = $_POST['timeslot'];
if (eregi('http:', $notes)) {
die ("Do NOT try that! ! ");
}
if(!$visitormail == "" && (!strstr($visitormail,"#") || !strstr($visitormail,".")))
{
echo "<h2>Use Back - Enter valid e-mail</h2>\n";
$badinput = "<h2>Please complete all the fields.</h2>\n";
echo $badinput;
die ("Use the Back button on your browser...");
}
if(empty($visitor) || empty($visitormail) || empty($notes ) || empty($attn ) || empty($phone )) {
echo "<h2>Please click Back and fill in all fields.</h2>\n";
die ("Use the Back button on your browser... ");
}
$todayis = date("l, F j, Y, g:i a") ;
$attn = $attn ;
$subject = $attn;
$notes = stripcslashes($notes);
$message = " $todayis [EST] \n
Web Site Request About: $attn \n
From: $visitor ($visitormail)\n
Phone: $phone \n
Message: $notes \n
Interested In: $interest \n
Heating Budget: $budget \n
Time Availability: Days - $day \n
Time Availability: Morning or Afternoon - $timeslot \n
Additional Info : IP = $ip \n
Browser Info: $httpagent \n
Referral : $httpref \n
";
$from = "From: $visitormail\r\n";
mail("XXXXX", $subject, $message, $from);
?>
And my HTML code is:
<?php
$ipi = getenv("REMOTE_ADDR");
$httprefi = getenv ("HTTP_REFERER");
$httpagenti = getenv ("HTTP_USER_AGENT");
?>
<input type="hidden" name="ip" value="<?php echo $ipi ?>" />
<input type="hidden" name="httpref" value="<?php echo $httprefi ?>" />
<input type="hidden" name="httpagent" value="<?php echo $httpagenti ?>" />
All fields are required.</p>
<p align="left">Your Name:
<input type="text" name="visitor" size="35" />
</p>
<p align="left">
Your Email:
<input type="text" name="visitormail" size="35" />
</p>
<p align="left">
<label for="Phone">Your Phone: </label>
<input type="text" name="Phone" id="Phone" size="35"/>
</span></p>
<p align="left">
About:
<select name="attn" size="1">
select name="interest" id="interest">
<option value="General Query">General question</option>
<option value="Plumbing Query">Plumbing</option>
<option value="Gasfitting Query">Gasfitting</option>
<option value="Central Heating Query">Central Heating</option>
<option value="Custom Work Query">Several options</option>
</select>
<br />
<p align="left">Your Home Heating Budget:
<select name="attn2" size="1">
select name="budget" id="budget">
<option value="Under 10000">$8000 to $10,0000</option>
<option value="10 to 12 Thousand">$10,000 to $12,000</option>
<option value="12 to 15 Thousand">$12,000 to $15,000</option>
<option value="+15000 Plus">$15,000 +</option>
</select>
<br />
<p align="left">Are you considering other forms of heating?
<input type="checkbox" name="Yes-OtherFormsHeating" id="Yes-OtherFormsHeating" />
<label for="Yes">Yes</label>
<input type="checkbox" name="No-OtherFormsHeating" id="No-OtherFormsHeating" />
<label for="No">No</label>
<br />
</p>
<p align="left">Please give us a few details about your home and requirements: <br />
<textarea name="notes" rows="6" cols="70"></textarea>
</p>
<p align="left">To give you a quote for gas central heating, we need to visit your home. What is a good day and time for us to come to you? Choose one option.</p>
<p align="left">
<input type="radio" name="day" id="day_M" value="M" />
<label for="M">M</label>
<input type="radio" name="day" id="day_T" value="T" />
<label for="T">T</label>
<input type="radio" name="day" id="day-W" value="W" />
<label for="W">W</label>
<input type="radio" name="day" id="day_Th" value="Th" />
<label for="Th">Th</label>
<input type="radio" name="day" id="day_F" value="F" />
<label for="F">F</label>
</p>
<p align="left">When is a good time on this day for us to come to you? Choose one or both.</p>
<p align="left">
<input type="checkbox" name="timeslot" id="8:30-12:30AM" />
<label for="8:30-12:30AM">8:30 - 12:30</label>
<input type="checkbox" name="timeslot" id="12:30-4" />
<label for="12:30-4">12:30 - 4:00</label>
</p>
First of all: always validate input (POST). You've got some basic validation already but read up on the security implications regarding POST data.
Array keys in php are case sensitive. $_POST['phone'] is not the same as $_POST['Phone'].
Your select tags are broken, take a look at the colouring in your posted html.
You have declared $budget twice, overwriting the intended data:
$budget = $_POST['budget'];
$budget = $_POST['day'];
checkboxes should be arrays when posted, change the name attribute to thename[] instead of just thename. Of course your php script must handle the resulting array after this change.

How do I find checkbox value in form sending $_POST (PHP)

I have this form:
<form action="../index_success.php" method="post" id="sendEmail" class="email">
<h3 class="register2">Newsletter Signup:</h3>
<ul class="forms email">
<li class="name">
<label for="yourName">Name: </label>
<input type="text" name="yourName" class="info" id="yourName" value="<?php echo $_POST['yourName']; ?>" /><br />
</li>
<li class="city"><label for="yourCity">City: </label>
<input type="text" name="yourCity" class="info" id="yourCity" value="<?php echo $_POST['yourCity']; ?>" /><br />
</li>
//This is where I need help with the check box code
<li class="classes"><label for="classInterest">Interested in classes?: </label>
<input type="checkbox" name="classInterest" class="info" id="classInterest" value="Yes" /><br />
</li>
<li class="email">
<label for="emailFrom">Email: </label>
<input type="text" name="emailFrom" class="info" id="emailFrom" value="<?php echo $_POST['emailFrom']; ?>" />
<?php if(isset($emailFromError)) echo '<span class="error">'.$emailFromError.'</span>';
?>
</li>
<li class="buttons email">
<button type="submit" id="submit">Send</button>
<input type="hidden" name="submitted" id="submitted" value="true" />
</li>
</ul>
</form>
This is emailed to a user. I don't know how to add the check box above so the user sees a "yes" if the box is checked:
<?php
$mailTo = 'xxx#xxx.com'; // This is the hardcoded Recipient Address
$mailSubject = 'Subject'; // This is the hardcoded Subject
$mailFrom = $_POST['emailFrom'];
$yourName = $_POST['yourName'];
$yourCity = $_POST['yourCity'];
$classInterest = $_POST['classInterest']; //This is the code for the checkbox
$mailHeader = "From: {$mailFrom}";
$mailBody = "Name = {$yourName} City = {$yourCity} Class interest = {$classInterest}";
mail( $mailTo , $mailSubject , $mailBody , $mailHeader );
Basically, what I need is (psudocode):
if ($classInterest == "yes") {
$classInterest = "Interested in Classes in ".$yourCity;
}
else {
...
}
Try this:
$classInterest = (isset($_POST['classInterest']) && $_POST['classInterest'] == 'Yes') ? "Interested in Classes in " . $yourCity : '';
EDIT
http://blog.gerv.net/2006/10/firefox_reload_behaviour/ read the article and first comment by Jason

Categories