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

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

Related

php send email form with attachment(s) validate the size and extensions

I have made this code and i have now run my head against the wall.
It seems that the code cannot valide neither the size or the extensions of the file!
I do receive the file and the email. Can someone please explain me why my validation fails?
<?php
//Get the uploaded file information
$name_of_uploaded_file =
basename($_FILES['uploaded_file']['name']);
//get the file extension of the file
$type_of_uploaded_file =
substr($name_of_uploaded_file,
strrpos($name_of_uploaded_file, '.') + 1);
$size_of_uploaded_file =
$_FILES["uploaded_file"]["size"]/1024;//size in KBs
//Settings
$max_allowed_file_size = 2000; // size in KB
$allowed_extensions = array("jpg", "jpeg", "gif", "bmp", "pdf");
$upload_folder = '../receivedfiles/';
//Validations
if($size_of_uploaded_file > $max_allowed_file_size )
{
$errors .= "\n Fejl: Filen er for stor";
}
//------ Validate the file extension -----
$allowed_ext = false;
for($i=0; $i<sizeof($allowed_extensions); $i++)
{
if(strcasecmp($allowed_extensions[$i],$type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
if(!$allowed_ext)
{
$errors .= "\n The uploaded file is not supported file type. ".
" Send venligst filer af følgende type: ".implode(',',$allowed_extensions);
}
//copy the temp. uploaded file to uploads folder
$path_of_uploaded_file = $upload_folder . $name_of_uploaded_file;
$tmp_path = $_FILES["uploaded_file"]["tmp_name"];
if(is_uploaded_file($tmp_path))
{
if(!copy($tmp_path,$path_of_uploaded_file))
{
$errors .= '\n error while copying the uploaded file';
}
}
$name = $_POST['name'];
$email = $_POST['email'];
$phone = $_POST['phone'];
$call = $_POST['call'];
$company = $_POST['company'];
$type = $_POST['type'];
$adress = $_POST['adress'];
$hesteid = $_POST['hesteid'];
$hestenavn = $_POST['hestenavn'];
$message = $_POST['message'];
$areacode = $_POST['areacode'];
$land = $_POST['land'];
$formcontent=" Fra: $company \n Navn: $name \n Adresse: $adress , $areacode \n Land: $land \n Telefon: $phone \n Ringes op: $call \n Type: $type \n Hoppens navn og ID: $hestenavn , $hesteid \n Besked: \n $message \n Vedhæftede filer: \n $path_of_uploaded_file";
$recipient = "my#email.dk";
$subject = "Besked fra hjemmesiden";
$mailheader = "Fra: $email \r\n";
mail($recipient, $subject, $formcontent, $mailheader) or die("Error!");
header('Location: /thank_you.shtml')
?>
HTML formcode:
<form class="contactform" action="php/mail.php" method="post" enctype="multipart/form-data">
<label for="company">Firma</label>
<input type="text" name="company" id="company" />
<label for="name">Navn</label>
<input type="text" name="name" placeholder="Dit navn" id="name" required="required" /><span class="red">*</span>
<br />
<label for="email">Email</label>
<input type="email" name="email" placeholder="din#mail.dk" id="email" required="required" /><span class="red">*</span>
<br />
<label for="phone">Telefon</label>
<input type="text" name="phone" placeholder="012345678" id="phone" required="required" /><span class="red">*</span>
<br />
<label for="adress">Adresse</label>
<input type="text" name="adress" placeholder="Din adresse" id="adress" />
<br />
<label for="areacode">Postnummer og By</label>
<input type="text" name="areacode" placeholder="Postnummer og By" id="areacode" />
<br />
<label for="land">Land</label>
<input type="text" name="land" placeholder="Land" id="land" />
<br />
<br />
<label for="call">Vil De ringes op?<span class="red">*</span></label>
<table class="callme">
<tr>
<td class="callmetext"><p>Ja</p></td>
<td class="callmecheck"><input type="radio" value="Ja" name="call" id="call" required="required" /></td>
</tr>
<tr>
<td class="callmetext"><p>Nej</p></td>
<td class="callmecheck"><input type="radio" value="Nej" name="call" /></td>
</tr>
</table>
<br />
<label for="type">Emne</label>
<select name="type" size="1" id="type" required="required">
<option value="">Vælg fra listen</option>
<option value="Bestille dekæning">Bestille bedækning</option>
<option value="Bestille brochure">Bestille brochure</option>
<option value="Information om en hingst">Information om en hingst</option>
<option value="Information om stutteriet">Information om stutteriet</option>
<option value="Information om salgsheste">Information om salgsheste</option>
<option value="Information om Afkomsformidling">Information om afkomsformidling</option>
<option value="Information om samarbejdspartnere">Information om vore samarbejdspartner</option>
<option value="Andet">Andet</option>
</select><span class="red">*</span>
<br />
<label for="hesteid">Hoppens ID</label>
<input type="text" name="hesteid" placeholder="208333DW..." id="hesteid" />
<br />
<label for="hestenavn">Hoppens navn</label>
<input type="text" name="hestenavn" placeholder="Hoppens navn..." id="hestenavn" />
<br />
<br />
<label for="message">Din besked<span class="red">*</span></label>
<textarea name="message" rows="6" cols="20" placeholder="Skriv din besked her..." id="message" required="required"></textarea>
<br />
<br />
<label for="uploaded_file">Vælg en fil:</label>
<input type="file" name="uploaded_file">
<br />
<br />
<div class="center">
<label for="captcha"> </label>
<div id="captcha" class="g-recaptcha" data-callback="recaptchaCallback" data-sitekey="6LfnbiATAAAAAOBV8B7qaPGzfpyjdahePpyGhLjj"></div>
</div>
<br />
<br />
<div class="center">
<label for="submitBtn"> </label>
<input class="contactbtn" type="submit" value="Send besked" id="submitBtn" disabled />
</div>
</form>
Bonus question: Will i be able to duplicate this code and change "uploaded_file" to "uploaded_file2", and 3, and so on and be able to add several more files?
There is no particular problem with your code. The only thing I noticed is that you aren't initializing $error anywhere, or checking its contents before sending the email.
Also, this entire block of code:
$allowed_ext = false;
for ($i = 0; $i < sizeof($allowed_extensions); $i++)
{
if (strcasecmp($allowed_extensions[$i], $type_of_uploaded_file) == 0)
{
$allowed_ext = true;
}
}
... can be simplified as:
$allowed_ext = in_array(strtolower($type_of_uploaded_file), $allowed_extensions);
And to answer the bonus question: To implement multiple file uploads, you can use an array for your input names:
<input type="file" name="uploaded_file[]">
<input type="file" name="uploaded_file[]">
<input type="file" name="uploaded_file[]">
Using print_r you can then figure out what this array looks like in PHP, and validate each file accordingly.
echo '<pre>'; print_r($_FILES); echo '</pre>';
After debugging, remember to remove any echo or print_r function calls that come before the header function. Otherwise you will receive the following error:
Warning: Cannot modify header information - headers already sent

Can't get php contact form to work

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

PHP form handling, checkbox array issue

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 />";
}

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.

Why is my form validation not working?

I'm having troubles validating my form. How do I validate the form using PHP? I've tried lots of different methods and nothing has worked. I can get the inputs to display (although check-box doesn't always display) but it just won't validate.
I also want to display the user's inputs (after it has been validated) onto another page, how do I do that?
Here is my code;
Form:
<form action="<?php $_SERVER['PHP_SELF'];?>" method="post">
<label for="name">Your Name:</label>
<input type="text" name="name" id="name" value="" required>
<br><br>
<label for="email">Your Email:</label>
<input type="text" name="email" id="email" value="" required>
<br>
<br>
<label for="subject">Subject:</label>
<input type="text" name="subject" id="subject" value="" required>
<br>
<br>
Recipient:
<div>
<label for="admin">
<input type="checkbox" name="recipient[]" id="admin" value="Administrator">
Administrator</label>
<br>
<label for="editor">
<input type="checkbox" name="recipient[]" id="editor" value="Content Editor">
Content Editor</label>
<br>
</div>
<br>
<label for="message">Message:</label>
<br>
<textarea name="message" id="message" cols="45" rows="5" required></textarea>
<input type="hidden" name="submitted" value="1">
<br>
<input type="submit" name="button" id="button" value="Send">
<br>
</form>
PHP:
<?php
$name = $_POST['name'];
$email = $_POST['email'];
$subject = $_POST['subject'];
$message = $_POST['message'];
if ($_POST['submitted']==1) {
if ($_POST['name']){
$name = $_POST['name'];
}
else{
echo "<p>Please enter a name.</p>" ;
}
if (preg_match("/^[_a-z0-9-]+(\.[_a-z0-9-]+)*#[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$/i", $email))
$email = $_POST['email'];
}
else{
echo "<p>Please enter a valid email.</p>";
}
if ($_POST['subject']){
$subject = $_POST['subject'];
}
else{
echo "<p>Please enter a subject.</p>";
if(empty($_POST['recipient'])){
echo "<p>Please select a recipient</p>";
}else{
for ($i=0; $i < count($_POST['recipient']);$i++) {
echo $_POST['recipient'][$i] . " ";
}
}
}
if ($_POST['message']){
$message = $_POST['message'];
}
/* go to form.php
display results
echo "<strong>Your Name:</strong> ".$name. "<br />";
echo "<strong>Your Email:</strong> ".$email. "<br />";
echo "<strong>Subject:</strong> ".$subject. "<br />";
echo "<strong>Recipient:</strong> ";
echo "<br />";
echo "<strong>Message:</strong> <br /> " .$message;
*/
?>
if (preg_match("/^[_a-z0-9-]+(.[_a-z0-9-]+)#[a-z0-9-]+(.[a-z0-9-]+)(.[a-z]{2,3})$/i", $email)
You can't use email in this condition because $email is empty!

Categories