How do I create required fields using POST & forms?
Posted: Mon May 03, 2010 6:38 am
Good afternoon,
I've created two pages to handle a request form that handles the following information: length width height name email and address. Once the form is filled out and submitted, it goes to a second page where it tells you if a) the dimensions will or will not fit b)if you didn't fill in your name, email or phone, you get a reminder to do so and c)sends an email of all info put into the form.
My two questions are this:
1. Can I just have 1 php page handle this instead of two?
2. How do write something so that if the contact information fields from the form aren't filled out, an email isn't generated, instead you're promted to fill out the req'd forms before continuing?
My code is below. THANK YOU so much for taking the time to peruse this.
The initial page that displays the form and submits the data:
The second page that handles the data, displays information and sends and email.
Javascript has been suggested but I don't currently use it (still new to all of this). Thanks for taking the time to look at this.
I've created two pages to handle a request form that handles the following information: length width height name email and address. Once the form is filled out and submitted, it goes to a second page where it tells you if a) the dimensions will or will not fit b)if you didn't fill in your name, email or phone, you get a reminder to do so and c)sends an email of all info put into the form.
My two questions are this:
1. Can I just have 1 php page handle this instead of two?
2. How do write something so that if the contact information fields from the form aren't filled out, an email isn't generated, instead you're promted to fill out the req'd forms before continuing?
My code is below. THANK YOU so much for taking the time to peruse this.
The initial page that displays the form and submits the data:
Code: Select all
<form action="accumen_request_handle.php" method="post">
<b>Describe the largest object which needs to be moved (in inches).</b> <br /><br />
Height? <input type = "text" name="height" type="text" size="2" maxlength="6" /> inches<p>
Length? <input type ="text" name="length" type="text" size="2" maxlength="6" /> inches<p>
Width? <input type ="text" name="width" type="text" size="2" maxlength="6" /> inches<p>
<br />
Client Name: <input type ="text" name="client_name" type="text" size="25" /><p></p>
Phone Number: <input type ="text" name="client_phone" type="text" size="25" maxlength="10" /><p></p>
E-mail Address: <input type ="text" name="client_email" type="text" size="25" /><p></p>
<input type="submit" name="submit" value="Submit Your Request" />
</form>Code: Select all
<?php //set variables from form
$height = $_POST["height"];
$width = $_POST ["width"];
$length = $_POST["length"];
$name = $_POST["client_name"];
$number = $_POST["client_phone"];
$email = $_POST["client_email"];
$strip_height = strip_tags($_POST['height']);
$strip_width = strip_tags($_POST['width']);
$strip_length = strip_tags($_POST['length']);
$strip_client_name = strip_tags($_POST['client_name']);
$strip_client_phone = strip_tags($_POST['client_phone']);
$strip_client_email = strip_tags($_POST['client_email']);
$total_height = 90;
$total_width = 91;
$total_length = 192;
if (($strip_height <= $total_height) and ($height) > 0 ){ //if height is less than total height
print("This will fit height-wise.");
}
elseif($strip_height >= $total_height) { //if height is more than total height
print("Sorry, but this won't fit because it's too tall.");
}
if (($strip_width <= $total_width) and ($width) > 0) { //if width is less than total width
print ("This will fit width-wise.");
}
elseif($strip_width >= $total_width) { //if width is more than total width
print("Sorry, but this won't fit because it's too wide.");
}
if (($strip_length <= $total_length) and ($length) > 0) { //if length is less than total length
print("This will fit length wise.");
}
elseif($strip_length >= $total_length) { //if length is more than total length
print("Sorry, but this won't fit because it's too long.");
}
//height?
if (empty($_POST['height'])) {
print '<p class="error">Please enter a height.</p>';
$okay = FALSE;
}
//width
if (empty($_POST['width'])) {
print '<p class="error">Please enter a width.</p>';
$okay = FALSE;
}
//length?
if (empty($_POST['length'])) {
print '<p class="error">Please enter a length.</p>';
$okay = FALSE;
}
//client name?
if (empty($_POST['client_name'])) {
print '<p class="error">Please enter your name.</p>';
$okay = FALSE;
}
//phone number?
if (empty($_POST['client_phone'])) {
print '<p class="error">Please enter your phone number.</p>';
$okay = FALSE;
}
//email address?
if (empty($_POST['client_email'])) {
print '<p class="error">Please enter your email address.</p>';
$okay = FALSE;
}
?>
<?php
$to = "emailaddress@gmail.com";
$subject = "Web Order Received";
//$body = "An order from the web site has been submitted.\n";
$content ="Height is $strip_height inches.\n Length is $strip_length inches.\n Width is $strip_width inches.\n Client Name is $strip_client_name.\n Client Email is $strip_client_email.\n Client phone number is $strip_client_phone";
if (mail($to, $subject,$content)) {
print("<p>Your order has been successfully sent! We'll be contacting you shortly.</p>");
} else {
print("<p>Your order has not been successfully sent!</p>");
}
?>
<br /> <br />
<?php //print current date and time:
//set the time zone:
date_default_timezone_set('America/New_York');
//now print the date and time:
print date('g:i a l F j');
print " EST"
?>