JoeAM Posted May 2, 2010 Share Posted May 2, 2010 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 intial page with the form: <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> The page that handles the form <?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 = "[email protected]"; $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" ?> Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/ Share on other sites More sharing options...
Ken2k7 Posted May 2, 2010 Share Posted May 2, 2010 Please read the rules, particularly #4 of Forum DOs. http://www.phpfreaks.com/page/rules-and-terms-of-service Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/#findComment-1052112 Share on other sites More sharing options...
JoeAM Posted May 2, 2010 Author Share Posted May 2, 2010 Did I note quote that this was PHP? Sorry, I'm fairly new to all of this and am unsure what I need/don't need to quote. Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/#findComment-1052142 Share on other sites More sharing options...
litebearer Posted May 2, 2010 Share Posted May 2, 2010 My 2cents... (fyi - there is a button - it will create 2 tags, place your code between the tags) Now to your question(s). 1. Yes, you can do it all on one page. My personal pref = 2 pages 2. Whether you use 1 page or two, start the pages wirth session_start. that way you can pass easily pass information (error messages, good data) back to the form. 3. Try to keep your error messages in an array before outputting them. It makes it easier (for me anyway) 4. Do all your error checking (when possible) near the top of your script - no need to do calculations or confrimation messages when later in the page errors may be found. its a start. Make sense? Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/#findComment-1052154 Share on other sites More sharing options...
JoeAM Posted May 3, 2010 Author Share Posted May 3, 2010 Thanks for the tips. I agree that two pages seems to be the way to go. It seems like there should be a way to stop the form from processing if it's missing key data (the name,email, and phone, for instance) but I've yet to find a way to do that. Some other pieces of code I've tried haven't worked because as soon as you go to the page it sees that those required fields are empty and you get an error message. Would using session_start solve this problem? Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/#findComment-1052189 Share on other sites More sharing options...
litebearer Posted May 3, 2010 Share Posted May 3, 2010 you could use javascript form validation on the client side. PHP validation would be on the server side, hence the necessity of error trapping and session variables Quote Link to comment https://forums.phpfreaks.com/topic/200494-creating-required-fields-when-using-forms-post/#findComment-1052192 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.