PHP form handling, checkbox array issue - php

I have a simple PHP script that handles an HTML form, and everything dumps to email correctly except for a multiple checkbox input. Instead of displaying the checkboxes a user selected, it displays Array.
<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST') {
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}
$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $gender = $_POST['gender']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $shirtsize = $_POST['shirt_size']; $country = $_POST['country']; $dayarea = $_POST['daytime_phone_area']; $dayphone = $_POST['daytime_phone']; $shirtsizeother = $_POST['other']; $dates = $_POST['dates']; $signature = $_POST['signature']; $signday = $_POST['signature_day']; $signmonth = $_POST['signature_month']; $signyear = $_POST['signature_year']; $position = $_POST['position']; $contactmethod = $_POST['contactmethod']; $other_size = 'n/a'; $_POST['other_size'] = 'n/a';
//Validate first
/*
Simple form validation
check to see if required fields were entered */ if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['gender'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['daytime_phone_area'] == "" || $_POST['daytime_phone'] == "" || $_POST['dates'] == "" || $_POST['signature'] == "" || $_POST['signature_day'] == "" || $_POST['signature_month'] == "" || $_POST['shirt_size'] == "" || $_POST['signature_year'] == "" || $_POST['position'] == "" || $_POST['contactmethod'] == "" ) {
var_dump($_POST);
echo "Please fill in all required boxes.";
}
else {
$email_from = 'chris569x#gmail.com';//<== update the email address $email_subject = "New Volunteer Registration"; $email_body = "You have received a new volunteer registration.\n".
"Volunteer: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Gender: $gender \n".
"Cell Phone: ($areacode) $cellphone \n".
"Daytime Phone: ($dayarea) $dayphone \n".
"T-Shirt Size: $shirtsize $shirtsizeother \n".
"Volunteer Position: $position \n".
"Dates availible to volunteer: $dates \n".
"Electronic Signature: $signature $signmonth/$signday/$signyear \n".
"Preferred contact method: $contactmethod \n"; $to = "chris569x#gmail.com";//<== update the email address $headers = "From: $email_from \r\n"; //Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thanks2.htm');
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?>
and the email output (problem is with dates availible to volunteer):
You have received a new volunteer registration.
Volunteer: xxxxxx
Address: xxxx
xxxxxx
Email: xxxx
Date of Birth: xxxx
Gender: xxxx
Cell Phone: xxxx
Daytime Phone: xxxx
T-Shirt Size: xxxx
Dates available to volunteer: Array
Electronic Signature: xxxx
Preferred contact method: xxxx
And here is the HTML form if you need to see it:
<form name="volunteer" action="form-to-email3.php" method="post">
<label>Last Name*: </label><input type="text" name="lastname" /><br /> <label>First Name*: </label><input type="text" name="firstname" /><br /> <label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br /> <label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("month", "day", 2); autojump("day", "year", 2); //--> </SCRIPT><br /><br /> <label>Gender*:</label><input type="radio" name="gender" value="Male"> Male <input type="radio" name="gender" value="Female"> Female </input> <br /> <br /> <label>Street Address*: </label><input type="text" name="streetaddress" /><br /> <label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input type="text" name="city" /><br />
<label>State/Province/Region*: </label><input type="text" name="state" /><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)"/><br />
<label>Country: </label>
<select class="element select medium" id="element_3_6" name="country">
</select> <br /><br />
<label>Email*: </label><input type="email" name="email" /><br /><br />
<label>Cell Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Daytime Phone*: </label>(<INPUT NAME="daytime_phone_area" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)"/>)<INPUT NAME="daytime_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"/><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("daytime_phone_area", "daytime_phone", 3); //--> </SCRIPT>
<label>T-Shirt Size*: </label><input type="radio" name="shirt_size" value="Small"> S <input type="radio" name="shirt_size" value="Medium"> M <input type="radio" name="shirt_size" value="Large"> L <input type="radio" name="shirt_size" value="XL"> XL <input type="radio" name="shirt_size" value="2XL"> 2XL <input type="radio" name="shirt_size" value="Other"> Other (Please provide)<br /> </input>
<label>Other: </label><input type="text" name="other size" /><br /><br /> <label>In which area will you be helping*: </label> <select name="position"> <option name="position" value= "Pastor">Pastor</option> <option name="position" value="Nursing Staff">Nursing Staff</option> <option name="position" value="Camp Staff">Camp Staff (Including Huddle Leaders)</option> <option name="position" value="Other Staff">Other Staff</option> </select><br /><br /> <label>Dates Attending*: <br />(Please check all dates you can be present)</label> <input type="checkbox" name="dates[]" value="7/6">Jul. 6 <input type="checkbox" name="dates[]" value="7/7">Jul. 7 <input type="checkbox" name="dates[]" value="7/8">Jul. 8 <input type="checkbox" name="dates[]" value="7/9">Jul. 9 <input type="checkbox" name="dates[]" value="7/10">Jul. 10 <input type="checkbox" name="dates[]" value="7/11">Jul. 11 <input type="checkbox" name="dates[]" value="7/12">Jul. 12<br /><br /><br /><br /></input> <label>What is the best way to contact you*? </label><input type="text" name="contactmethod" /><br /><br /><br />
<b>Electronic Submission</b>
<p>I understand that by filling in my name here, it shall act as a binding, legal signagure. </p> <label>Electronic Signagure*: </label><input type="text" name="signature" /><br />
<label>Date*: </label><INPUT NAME="signature_month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)"/>/<INPUT NAME="signature_year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)"/>
<SCRIPT TYPE="text/javascript">
<!--
autojump("signature_month", "signature_day", 2); autojump("signature_day", "signature_year", 2); //--> </SCRIPT><br /><br /><br /><br /><br />
<input type="submit" value="Submit" />
</form>

Try using PHP's implode() function and changing
$dates = $_POST['dates'];
to
$dates = implode(", ",$_POST['dates']);
This will convert the array into a string where each date is separated by a comma.

When you used the name dates[] in your HTML forms, you specified that the data should be in an array format. The [] syntax tells the form to create a new element in the array of your POST data.
To step through the values of your array, you'd use foreach:
foreach($dates as $v) {
echo $v . "<br />";
}

Related

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

PHP error on all fields required - but all fields are inserted

I'm making a signup form (signup.html) with a php handler (signup-form-handler.php).
But it seems like I'm doing something wrong.
This is my code:
$errors = '';
$myemail = 'example#domain.com';//<-----Put Your email address here.
if(empty($_POST['FirstName']) ||
empty($_POST['LastName']) ||
empty($_POST['City']) ||
empty($_POST['Country']))
{
$errors .= "\n Error: all fields are required";
}
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$City = $_POST['City'];
$Country = $_POST['Country'];
$Int1 = $_POST['Int1']; // <---- Here's what seems to be the issue
$Int2 = $_POST['Int2']; // <---- Here's what seems to be the issue
if( empty($errors))
{
$to = $myemail;
$email_subject = "Udfyldelse af test på Gamification: $FirstName";
$email_body = "Du har fået en ny besvarelse. ".
"Her er besvarelserne:\n Fornavn: $FirstName \n Efternavn: $LastName \n By: $City \n Land $Country \n".
"De fire personlige interesser: $Int1 og $Int2";
$headers = "From: $myemail\n";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
Here's the relevant html code:
<form id="contact_form" name="contact_form" action="signup-form-handler.php">
<!-- Personlige oplysninger -->
<div class="next">
<h3>Personlige oplysninger<a class="info" href="#" title="Testing"><img style="height: 15px; width: 15px; margin-left: 5px;" alt="Time" src="img/signup_form/info_simple.png"></a></h3>
<b>Mit navn er</b>
<input class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" type="text" maxlength="60" />
<input class="user_field" placeholder="efternavn" id="LastName" name="LastName" type="text" maxlength="60" />
</br>
<b>og bor i</b>
<input class="user_field" placeholder="indsæt by" id="City" name="City" type="text" maxlength="60" />
<b>,</b>
<input class="user_field" placeholder="indsæt land" id="Country" name="Country" type="text" maxlength="60" />
</br>
</br>
<b>Beskriv dig selv med 4 interesser</b>
</br>
<input id="Int1" name="Int1" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 1" />
<input id="Int2" name="Int2" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 2" />
<input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 3" />
<input class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 4" />
</div>
As you can see from my code example, I'm trying to get input from my an html form. I've commented in to the code, where the problem occurs. If I retrieve input from Int1 and Int2, I get the answer, that all required fields need to be filled in, but I don't want to require Int1 and Int2. Also I didn't add Int1 and Int2 in the beginning where I check if the other variables are empty.
If I remove Int1 and Int2 it all works fine.
When I send the form after inserting all the inputs, I get the following message:
"Error: all fields are required"
What am I doing wrong?
Hope I gave enough info to find the issue, thanks!
The default method is GET. Set your form method to POST:
<form id="contact_form" name="contact_form" action="signup-form-handler.php" method="POST">
So basically you're not passing the values to the script. If you wanted to use the GET method, switch all instances of $_POST to $_GET and you can leave your form as is.
In the inputs Int1 and Int2 the attribute type is missing.
ok, for starters, the "< b >" for bold tag is depricated, use "< strong > < /strong > "
if you add this to your php page it works, play about with the form though as i couldnt understand the danish to know what was what. this is a one page solution, no need for a HTML file
<?php
$form = '<form id="contact_form" name="contact_form" action="'.$_SERVER['PHP_SELF'].'" method="post" >
<!-- Personlige oplysninger -->
<h3>Personlige oplysninger<a class="info" href="#" title="Testing"><img style="height: 15px; width: 15px; margin-left: 5px;" alt="Time" src="img/signup_form/info_simple.png"></a></h3>
<strong>Mit navn er</strong>
<input class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" type="text" maxlength="60">
<input class="user_field" placeholder="efternavn" id="LastName" name="LastName" type="text" maxlength="60" >
<input type="text" class="user_field" placeholder="fornavn" id="FirstName" name="FirstName" >
<strong>og bor i</strong>
<input class="user_field" placeholder="indsæt by" id="City" name="City" type="text" maxlength="60" >
<strong>,</strong>
<input class="user_field" placeholder="indsæt land" id="Country" name="Country" type="text" maxlength="60" >
<strong>Beskriv dig selv med 4 interesser</strong>
<input type="text" id="Int1" name="Int1" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 1" >
<input type="text" id="Int2" name="Int2" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 2" >
<input type="text" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 3" >
<input type="text" class="tag_btn user_field" style="color: #FFFFFF;" placeholder="Interesser 4" >
<input type="submit" name="submit" value="Enter">
</form>';
if (isset ($_POST['submit'])){
$errors = '';
$myemail = 'example#domain.com';//<-----Put Your email address here.
if(empty($_POST['FirstName']) ||
empty($_POST['LastName']) ||
empty($_POST['City']) ||
empty($_POST['Country']))
{
$errors .= "\n Error: all fields are required";
$display = $errors.$form;
}else{
$FirstName = $_POST['FirstName'];
$LastName = $_POST['LastName'];
$City = $_POST['City'];
$Country = $_POST['Country'];
$Int1 = $_POST['Int1']; // <---- Here's what seems to be the issue
$Int2 = $_POST['Int2']; // <---- Here's what seems to be the issue
$to = $myemail;
$email_subject = "Udfyldelse af test på Gamification: $FirstName";
$email_body = "Du har fået en ny besvarelse. ".
"Her er besvarelserne:\n Fornavn: $FirstName \n Efternavn: $LastName \n By: $City \n Land $Country \n".
"De fire personlige interesser: $Int1 og $Int2";
$headers = "From: $myemail\n";
mail($to,$email_subject,$email_body,$headers);
//redirect to the 'thank you' page
header('Location: contact-form-thank-you.html');
}
}else{
$display = $errors.$form;
}
echo $display;
?>

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.

PHP POST form validation error

I have an HTML form:
<div id="registration">
<p> SGU is for youth who have completed 6th through 12th grades. Please complete this entire form for EACH CAMPER. (Download Form to Print)<br />
<br />
* Denotes required field</p>
<form action="form-to-email.php" method="post" name="camper_registration" id="camper registration">
<label>Last Name*: </label>
<input name="lastname" type="text" id="lastname" required="required"/>
<br />
<label>First Name*: </label><input name="firstname" type="text" id="firstname" required="required"/><br />
<label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br />
<label>Street Address*: </label><input name="streetaddress" type="text" id="streetaddress" required="required"/><br />
<label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input name="city" type="text" id="city" required="required"/><br />
<label>State/Province/Region*: </label><input name="state" type="text" id="state" required="required"/><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)" required="required"><br /><br />
<label>Youth's Email*: </label><input type="email" name="email" id="email" required="required"/><br /><br />
<label>Youth's Cell*: </label>(<input type="tel" name="youth_area" size="3" maxlength="3" onKeyPress="return numbersonly(this, event)" required="required" />) <input type="tel" name="youth_phone" size="7" maxlength="7" onKeyPress="return numbersonly(this, event)" required="required" /><SCRIPT TYPE="text/javascript">
<!--
autojump("youth_area", "youth_phone", 3);
//-->
</SCRIPT><br /><br />
<label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)" required="required">/
<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)" required="required">/
<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)" required="required">
<SCRIPT TYPE="text/javascript">
autojump("month", "day", 2); autojump("day", "year", 2);
</SCRIPT><br /><br />
<label>Grade completed in<br /> Spring 2013*: </label><input type="tel" name="grade" size=2 maxlength=2 required="required"/><br /><br />
<label>Gender*:</label><input type="radio" name="gender" value="Male" required="required"> Male
<input type="radio" name="gender" value="Female" required="required"> Female
<br /> <br />
<label>Parent/Guardian(s) First Name*: </label><input name="guardian_first" type="text" id="guardian_first" required="required"/><br /><br />
<label>Parent/Guardian(s) Last Name*: </label><input name="guardian_last" type="text" id="guardian_last" required="required"/> <br /><br />
<label>Parent Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)" required="required"><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Parent/Guardian Email*: </label><input name="parent_email" type="email" id="parent_email" required="required"/><br />
<b>This email will be used for confirmation of <br />
registration and all future contact</b><br /><br />
<label>1st Emergency Contact*: </label><input name="emergency_contact_1" type="text" id="emergency_contact_1" required="required"/><br />
<label>Contact Number*: </label>(<INPUT NAME="emergency_contact_1_areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="emergency_contact_1_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)" required="required"><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("emergency_contact_1_areacode", "emergency_contact_1_phone", 3);
//-->
</SCRIPT>
<label>2nd Emergency Contact*: </label><input name="emergency_contact_2" type="text" id="emergency_contact_2" required="required"/><br />
<label>Contact Number*: </label>(<INPUT NAME="emergency_contact_2_areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)" required="required">)<INPUT NAME="emergency_contact_2_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)" required="required"><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("emergency_contact_2_areacode", "emergency_contact_2_phone", 3);
//-->
</SCRIPT>
<label>Name of Home Church: </label><input type="text" name="home_church" /><br />
<label>Phone Number: </label>(<INPUT NAME="church_areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)">)<INPUT NAME="church_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"><br />
<SCRIPT TYPE="text/javascript">
autojump("church_areacode", "church_phone", 3);
</SCRIPT>
<label>Contact Person: </label><input type="text" name="contact_person" /><br /><br />
<b>Special Needs</b><br />
Some campers may have needs that might require special attention from our staff; accessibility, health concerns, diet, allergies, etc. <br /><br />
<label>Please list any special needs: </label>
<textarea rows="10" cols="20" name="special_needs">
</textarea> <br /><br />
<label>T-Shirt Size*: </label><input type="radio" name="shirt size" value="Small" required="required"> Small
<input type="radio" name="shirt size" value="Medium" required="required"> Medium
<input type="radio" name="shirt size" value="Large" required="required"> Large
<input type="radio" name="shirt size" value="XL" required="required"> XL
<input type="radio" name="shirt size" value="2XL" required="required"> 2XL<br /><br />
<b>Roommate</b><br />
There are double and many single occupancy dorm rooms at Grinnell college campus - if possible we will honor your request for ONE preferred roommate.<br /><br />
<label>Roommate Preference: </label>
<input type="text" name="roommate" /><br /><br />
<div id="satellites">
<p><b>Satellite Choices</b>
List your first, second, and third choices. You will be given your first choice if it is not full. ALL events have limited capacity. If you do not choose a satellite, one will be assigned for you.<br /><br />
<label>First Choice*: </label>
<select name="satellite1" id="satellite1" required="required" onchange="clearOthers()">
<option value="" disabled>Select</option>
<option value="art">Art</option>
<option value="basketball">Basketball</option>
<option value="dance">Dance</option>
<option value="drama">Drama</option>
<option value="football">Football</option>
<option value="missions">Missions</option>
<option value="photo1">Landscape & Still Life Photography</option>
<option value="photo2">People Photography</option>
<option value="scrap-booking">Scrap-Booking</option>
<option value="sgjr">Summer Games Jr. Training</option>
<option value="soccer">Soccer</option>
<option value="ultimate">Ultimate Frisbee</option>
<option value="video_games">Video Games</option>
<option value="volleyball">Volleyball</option>
<option value="water_park">Water Park</option>
</select><br />
<label>Second Choice*: </label>
<select name="satellite2" id="satellite2" required="required">
<option value="" disabled>Select</option>
<option value="art">Art</option>
<option value="basketball">Basketball</option>
<option value="dance">Dance</option>
<option value="drama">Drama</option>
<option value="football">Football</option>
<option value="missions">Missions</option>
<option value="photo1">Landscape & Still Life Photography</option>
<option value="photo2">People Photography</option>
<option value="scrap-booking">Scrap-Booking</option>
<option value="sgjr">Summer Games Jr. Training</option>
<option value="soccer">Soccer</option>
<option value="ultimate">Ultimate Frisbee</option>
<option value="video_games">Video Games</option>
<option value="volleyball">Volleyball</option>
<option value="water_park">Water Park</option>
</select><br />
<label>Third Choice*: </label>
<select name="satellite3" id="satellite3" required="required">
<option value="" disabled>Select</option>
<option value="art">Art</option>
<option value="basketball">Basketball</option>
<option value="dance">Dance</option>
<option value="drama">Drama</option>
<option value="football">Football</option>
<option value="missions">Missions</option>
<option value="photo1">Landscape & Still Life Photography</option>
<option value="photo2">People Photography</option>
<option value="scrap-booking">Scrap-Booking</option>
<option value="sgjr">Summer Games Jr. Training</option>
<option value="soccer">Soccer</option>
<option value="ultimate">Ultimate Frisbee</option>
<option value="video_games">Video Games</option>
<option value="volleyball">Volleyball</option>
<option value="water_park">Water Park</option>
</select>
<script type="text/javascript">
function clearOthers() {
document.getElementById("secondchoice").options.selectedIndex=0;
document.getElementById("thirdchoice").options.selectedIndex=0;
}
</script>
</div>
And then process it with this PHP:
<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST')
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}
$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $grade = $_POST['grade']; $gender = $_POST['gender']; $guardian_first = $_POST['guardian_first']; $guardian_last = $_POST['guardian_last']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $contact1 = $_POST['emergency_contact_1']; $contact1areacode = $_POST['emergency_contact_1_areacode']; $contact1phone = $_POST['emergency_contact_1_phone']; $contact2 = $_POST['emergency_contact_2']; $contact2areacode = $_POST['emergency_contact_2_areacode']; $contact2phone = $_POST['emergency_contact_2_phone']; $homechurch = $_POST['home_church']; $churchareacode = $_POST['church_areacode']; $churchphone = $_POST['church_phone']; $contactperson = $_POST['contact_person']; $specialneeds = $_POST['special_needs']; $shirtsize = $_POST['shirt size']; $roommate = $_POST['roommate']; $firstchoice = $_POST['satellite1']; $secondchoice = $_POST['satellite2']; $thirdchoice = $_POST['satellite3']; $youth_area = $_POST['youth_area']; $youth_phone = $_POST['youth_phone'];
//Validate first
/*
Simple form validation
check to see if required fields were entered
*/
if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['grade'] == "" || $_POST['gender'] == "" || $_POST['guardian_first'] == "" || $_POST['guardian_last'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['emergency_contact_1'] == "" || $_POST['emergency_contact_1_areacode'] == "" || $_POST['emergency_contact_1_phone'] == "" || $_POST['emergency_contact_2'] == "" || $_POST['emergency_contact_2_areacode'] == "" || $_POST['emergency_contact_2_phone'] == "" || $_POST['shirt size'] == "" || $_POST['satellite1'] == "" || $_POST['satellite2'] == "" || $_POST['satellite3'] == "" || $_POST['youth_area'] == "" || $_POST['youth_phone'] == "" ) {
echo "Please fill in all required boxes. \n";
var_dump($_POST);}
else {
$email_from = 'chris569x#gmail.com';//<== update the email address
$email_subject = "New Registration";
$email_body = "You have received a new registration.\n".
"Camper: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Grade Completed: $grade \n".
"Gender: $gender \n".
"Youth Cell Phone: ($youth_area) $youth_phone \n".
"Guardian: $guardian_first $guardian_last \n".
"Guardian Cell Phone: ($areacode) $cellphone \n".
"First Emergency Contact: $contact1 Contact Number: ($contact1areacode) $contact1phone \n".
"Second Emergency Contact: $contact2 Contact Number: ($contact2areacode) $contact2phone \n".
"Home Church: $homechurch Contact Number: ($churchareacode) $churchphone Contact Person: $contactperson \n".
"Special Needs: $specialneeds \n".
"T-Shirt Size: $shirtsize \n".
"Roommate Preference: $roommate \n".
"Satellite Preferences: 1.$firstchoice 2.$secondchoice 3.$thirdchoice \n".
" \n";
$to = "elaineknudtson#gmail.com,chris569x#gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header("Location: thanks2.htm",303);
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?>
I included the var_dump because I keep getting my "Please fill in all required boxes." error. When I look through the dump, there are no strings without a value. I am very confused, where is the disconnect?
var_dump:
Please fill in all required boxes. array(38) { ["lastname"]=> string(4) "test" ["firstname"]=> string(4) "test" ["initial"]=> string(1) "t" ["streetaddress"]=> string(8) "123 test" ["addressline2"]=> string(4) "test" ["city"]=> string(9) "testville" ["state"]=> string(10) "test state" ["zip"]=> string(5) "12345" ["email"]=> string(13) "test#test.com" ["youth_area"]=> string(3) "123" ["youth_phone"]=> string(7) "1234566" ["month"]=> string(2) "21" ["day"]=> string(2) "21" ["year"]=> string(4) "2121" ["grade"]=> string(2) "12" ["gender"]=> string(4) "Male" ["guardian_first"]=> string(9) "test test" ["guardian_last"]=> string(9) "test test" ["areacode"]=> string(3) "123" ["cellphone"]=> string(7) "1231231" ["parent_email"]=> string(13) "test#test.com" ["emergency_contact_1"]=> string(9) "test test" ["emergency_contact_1_areacode"]=> string(3) "123" ["emergency_contact_1_phone"]=> string(7) "1231231" ["emergency_contact_2"]=> string(10) "test testy" ["emergency_contact_2_areacode"]=> string(3) "123" ["emergency_contact_2_phone"]=> string(7) "1232132" ["home_church"]=> string(11) "church test" ["church_areacode"]=> string(3) "321" ["church_phone"]=> string(7) "6548976" ["contact_person"]=> string(12) "tester tests" ["special_needs"]=> string(13) "none. testing" ["shirt_size"]=> string(2) "XL" ["roommate"]=> string(4) "none" ["satellite1"]=> string(5) "drama" ["satellite2"]=> string(6) "photo1" ["satellite3"]=> string(10) "volleyball" ["submit"]=> string(6) "Submit" }
you missed an underscore in the if:
if ($_POST['firstname'] == "" ||
the $_POST['shirt size'] should be shirt_size.
^
This happened to you because your approach is not elegant, that huge If is nasty nasty nasty xD.
You should have a look to foreach() learning to loop through arrays. Also have a look to empty() and isset()
in the php documentation, those are your first friends for validating.
How was it made:
Basicly to search your error I copy pasted your if clause, and the var_dumped array, used some regex magic on them so they became valid php arrays:
$arr = array("lastname"=>"test", "firstname"=>"test", "initial"=>"t", "streetaddress"=>"123 test", "addressline2"=>"test", "city"=>"testville", "state"=>"test state", "zip"=>"12345", "email"=>"test#test.com", "youth_area"=>"123", "youth_phone"=>"1234566", "month"=>"21", "day"=>"21", "year"=>"2121", "grade"=>"12", "gender"=>"Male", "guardian_first"=>"test test", "guardian_last"=>"test test", "areacode"=>"123", "cellphone"=>"1231231", "parent_email"=>"test#test.com", "emergency_contact_1"=>"test test", "emergency_contact_1_areacode"=>"123", "emergency_contact_1_phone"=>"1231231", "emergency_contact_2"=>"test testy", "emergency_contact_2_areacode"=>"123", "emergency_contact_2_phone"=>"1232132", "home_church"=>"church test", "church_areacode"=>"321", "church_phone"=>"6548976", "contact_person"=>"tester tests", "special_needs"=>"none. testing", "shirt_size"=>"XL", "roommate"=>"none", "satellite1"=>"drama", "satellite2"=>"photo1", "satellite3"=>"volleyball", "submit"=>"Submit");
$arr2 = array('firstname', 'lastname', 'streetaddress', 'city', 'state', 'zip', 'email', 'month', 'day', 'year', 'grade', 'gender', 'guardian_first', 'guardian_last', 'areacode', 'cellphone', 'emergency_contact_1', 'emergency_contact_1_areacode', 'emergency_contact_1_phone', 'emergency_contact_2', 'emergency_contact_2_areacode', 'emergency_contact_2_phone', 'shirt size', 'satellite1', 'satellite2', 'satellite3', 'youth_area', 'youth_phone');
Next time please use var_export() so you output a valid php array and people can just copypaste it!!
Then i used this loop to find out if they matched:
foreach($arr2 as $k=>$v){
if(!array_key_exists($v, $arr)){
echo "$v not in array 1 <br/>";
}
}
Output: shirt size not in array 1
Enjoy!
<?php
if (isset($_POST['submit'])) {
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
if (empty($firstname == '' || $lastname == '')) {
echo "empty";
}
}
?>
Or somthing like this :
// Required field names
$required = array('login', 'password', 'confirm', 'name', 'phone', 'email');
// Loop over field names, make sure each one exists and is not empty
$error = false;
foreach($required as $field) {
if (empty($_POST[$field])) {
$error = true;
}
}
if ($error) {
echo "All fields are required.";
} else {
echo "Proceed...";
}
Please use
if(empty($_POST['firstname']) && $_POST['firstname'])
instead of :
if($_POST['firstname']=="")
for each of the field.Your array is very big you may have missed any field , the empty() will check whether it has been set or not & next statement will check it is nonzero.
for more about empty()
hope it will help!

php form validation issue, "fill in all required boxes"

I have created a simple HTML form, and process it using PHP.
When I try to submit the form, it is giving me the error message I created for not filling in all the required boxes. I have checked and rechecked the variables, and I cannot figure out why it is giving me that message with all the fields filled in.
HTML:
<form name="volunteer" action="form-to-email3.php" method="post">
<label>Last Name*: </label><input type="text" name="lastname" /><br />
<label>First Name*: </label><input type="text" name="firstname" /><br />
<label>Middle Initial: </label><input type="text" name="initial" size=1 maxlength=1 /><br /><br />
<label>Date of Birth*: </label><INPUT NAME="month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)">
<SCRIPT TYPE="text/javascript">
<!--
autojump("month", "day", 2); autojump("day", "year", 2);
//-->
</SCRIPT><br /><br />
<label>Gender*:</label><input type="radio" name="gender" value="Male"> Male
<input type="radio" name="gender" value="Female"> Female
<br /> <br />
<label>Street Address*: </label><input type="text" name="streetaddress" /><br />
<label>Address Line 2: </label><input type="text" name="addressline2" /><br />
<label>City*: </label><input type="text" name="city" /><br />
<label>State/Province/Region*: </label><input type="text" name="state" /><br />
<label>Zipcode*: </label>
<INPUT NAME="zip" input type="tel" SIZE=5 MAXLENGTH=5
onKeyPress="return numbersonly(this, event)"><br />
<label>Country: </label>
<select class="element select medium" id="element_3_6" name="country">
<option value="" selected="selected"></option>
<option value="Afghanistan" >Afghanistan</option>
<option value="Albania" >Albania</option>
</select>
<br /><br />
<label>Email*: </label><input type="email" name="email" /><br /><br />
<label>Cell Phone*: </label>(<INPUT NAME="areacode" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)">)<INPUT NAME="cellphone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("areacode", "cellphone", 3);
//-->
</SCRIPT>
<label>Daytime Phone*: </label>(<INPUT NAME="daytime_phone_area" input type="tel" SIZE=3 MAXLENGTH=3
onKeyPress="return numbersonly(this, event)">)<INPUT NAME="daytime_phone" input type="tel"SIZE=7 MAXLENGTH=7
onKeyPress="return numbersonly(this, event)"><br /><br />
<SCRIPT TYPE="text/javascript">
<!--
autojump("daytime_phone_area", "daytime_phone", 3);
//-->
</SCRIPT>
<label>T-Shirt Size*: </label><input type="radio" name="shirt_size" value="Small"> Small
<input type="radio" name="shirt_size" value="Medium"> Medium
<input type="radio" name="shirt_size" value="Large"> Large
<input type="radio" name="shirt_size" value="XL"> XL
<input type="radio" name="shirt_size" value="2XL"> 2XL
<input type="radio" name="shirt_size" value="Other"> Other (Please provide)<br />
<label>Other:</label><input type="text" name="other size" /><br /><br />
<p>In which area will you be helping*:</p>
<select>
<option name= "position" value= "Pastor">Pastor</option>
<option name= "position" value="Nursing Staff">Nursing Staff</option>
<option name= "position" value="Camp Staff">Camp Staff (Including Huddle Leaders)</option>
<option name= "position" value="Other Staff">Other Staff</option>
</select><br /><br />
<label>Dates Attending*: <br />(Please check all dates you can be present)</label>
<input type="checkbox" name="dates" value="7/6">July 6
<input type="checkbox" name="dates" value="7/7" />July 7
<input type="checkbox" name="dates" value="7/8">July 8
<input type="checkbox" name="dates" value="7/9">July 9
<input type="checkbox" name="dates" value="7/10">July 10
<input type="checkbox" name="dates" value="7/11">July 11
<input type="checkbox" name="dates" value="7/12">July 12<br /><br /><br /><br />
<label>What is the best way to contact you*? </label><input type="text" name="contactmethod" /><br /><br /><br />
<b>Electronic Submission</b>
<p>I understand that by filling in my name here, it shall act as a binding, legal signagure. </p>
<label>Electronic Signagure*: </label><input type="text" name="signature" /><br />
<label>Date*: </label><INPUT NAME="signature_month" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="signature_day" input type="tel" SIZE=2 MAXLENGTH=2
onKeyPress="return numbersonly(this, event)">/<INPUT NAME="signature_year" input type="tel" SIZE=4 MAXLENGTH=4
onKeyPress="return numbersonly(this, event)">
<SCRIPT TYPE="text/javascript">
<!--
autojump("signature_month", "signature_day", 2); autojump("signature_day", "signature_year", 2);
//-->
</SCRIPT><br /><br /><br /><br /><br />
<input type="submit" value="Submit">
</form>
PHP:
<?php
if($_SERVER['REQUEST_METHOD'] !== 'POST')
{
//This page should not be accessed directly. Need to submit the form.
echo "error; you need to submit the form!";
die;
}
$firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $initial = $_POST['initial']; $streetaddress = $_POST['streetaddress']; $addressline2 = $_POST['addressline2']; $city = $_POST['city']; $state = $_POST['state']; $zip = $_POST['zip']; $email = $_POST['email']; $month = $_POST['month']; $day = $_POST['day']; $year = $_POST['year']; $gender = $_POST['gender']; $areacode = $_POST['areacode']; $cellphone = $_POST['cellphone']; $shirtsize = $_POST['shirt_size']; $country = $_POST['country']; $dayarea = $_POST['daytime_phone_area']; $dayphone = $_POST['daytime_phone']; $shirtsizeother = $_POST['other']; $dates = $_POST['dates']; $signature = $_POST['signature']; $signday = $_POST['signature_day']; $signmonth = $_POST['signature_month']; $signyear = $_POST['signature_year']; $position = $_POST['position']; $contactmethod = $_POST['contactmethod'];
//Validate first
/*
Simple form validation
check to see if required fields were entered
*/
if ($_POST['firstname'] == "" || $_POST['lastname'] == "" || $_POST['streetaddress'] == "" || $_POST['city'] == "" || $_POST['state'] == "" || $_POST['zip'] == "" || $_POST['email'] == "" || $_POST['month'] == "" || $_POST['day'] == "" || $_POST['year'] == "" || $_POST['gender'] == "" || $_POST['areacode'] == "" || $_POST['cellphone'] == "" || $_POST['daytime_phone_area'] == "" || $_POST['daytime_phone'] == "" || $_POST['dates'] == "" || $_POST['signature'] == "" || $_POST['signature_day'] == "" || $_POST['signature_month'] == "" || $_POST['shirt_size'] == "" || $_POST['signature_year'] == "" || $_POST['position'] == "" || $_POST['contactmethod'] == "" ) {
echo "Please fill in all required boxes.";}
else {
$email_from = 'chris569x#gmail.com';//<== update the email address
$email_subject = "New Volunteer Registration"; $email_body = "You have received a new volunteer registration.\n".
"Volunteer: $firstname $initial $lastname \n".
"Address: $streetaddress \n".
"$addressline2 \n".
"$city, $state $zip \n".
"Email: $email \n".
"Date of Birth: $month/$day/$year \n".
"Gender: $gender \n".
"Cell Phone: ($areacode) $cellphone \n".
"Daytime Phone: ($dayarea) $dayphone \n".
"T-Shirt Size: $shirtsize $shirtsizeother \n".
"Dates availible to volunteer: $dates \n".
"Electronic Signature: $signature $signmonth/$signday/$signyear \n".
"Preferred contact method: $contactmethod \n";
$to = "chris569x#gmail.com";//<== update the email address
$headers = "From: $email_from \r\n";
//Send the email!
mail($to,$email_subject,$email_body,$headers);
//done. redirect to thank-you page.
header('Location: thanks2.htm');
echo "<meta http-equiv='refresh' content='0; url=thanks2.htm'>";
}
?>
I believe that you need to change your select for position.
It appears that the name is not coming over to the post page because you put the name in instead of the . Fix that and you should be good to go.
Congratulations, your server seems to be properly configured! The variables $city should not hold any data. You have to use $_POST['city'] or $_REQUEST['city'] instead.
Some hosts may still support submitting variables directly to the $variables, but this is not a safe configuration.
For instance:
...
if (password_check($username,$password)) {
$access = 'yes'
}
...
if ('yes'==$access) { ... }
Could be hacked by submitting script.php&access=yes
So you have to use $_POST['variable'] directly or copy it in the $variable, first.

Categories