JoeAM Posted May 3, 2010 Share Posted May 3, 2010 Is it possible to prevent a form from processing if required fields aren't filled out on the HTML portion of the script? In other words, I have an .html file that displays the form and a .php file that displays some results as well as sends an email detailing the results. However, I'd like to STOP the form from ever getting to the .php page if certain fields (name, email, phone number) aren't entered into the form on the .html side. Is that do-able? Thank you very much for looking at this. <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> <?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/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/ Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 Not exactly, but you can have checks in the php that will bounce back a message and the original form page if certain fileds are left blank. You can also use session variables or a hidden form on your php page to refill the information that was originaly entered on the form when it reloads. Check out the php manual for "isset()" for the best way to check empty variables. Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052313 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 Yes, you can, but not with PHP, you use Javascript; Instead of using a 'type=submit' input element, you use a generic 'type=button' input element. Then you attach a javascript function to the 'onSubmit' Event property of the Button. That function will check and validate variables before, itself, submitting the form. More Info: http://www.elated.com/articles/form-validation-with-javascript/ -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052314 Share on other sites More sharing options...
JoeAM Posted May 3, 2010 Author Share Posted May 3, 2010 Not exactly, but you can have checks in the php that will bounce back a message and the original form page if certain fileds are left blank. You can also use session variables or a hidden form on your php page to refill the information that was originaly entered on the form when it reloads. Check out the php manual for "isset()" for the best way to check empty variables. I know that isset() will probably come into play here but I just can't figure out where it needs to go. If i condense both pages into one and have the form call itself, I know that I can use isset() there. But this is the longest code I've written and I keep getting lost in the myriad if/then statements I've written. Yes, you can, but not with PHP, you use Javascript; Instead of using a 'type=submit' input element, you use a generic 'type=button' input element. Then you attach a javascript function to the 'onSubmit' Event property of the Button. That function will check and validate variables before, itself, submitting the form. More Info: http://www.elated.com/articles/form-validation-with-javascript/ -cb- Thanks for the tip, but I'm trying to do this with just php and html. I haven't even started looking into javascript yet. But you're saying that what I'm looking to do is technically not possible if I don't use JS? Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052319 Share on other sites More sharing options...
ChemicalBliss Posted May 3, 2010 Share Posted May 3, 2010 If you dont want the page to reload, then you must use javascript. PHP is run on the server, and as such the client needs to make requests for a php script. If you dont mind just reloading the form, then php is your man. (You can do this by having the processing part, and the form part in the same php script. Therefor you can populate values from the subbmitted request if the validation failed). -cb- Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052338 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 ok, here's the quick and dirty play on it missing out the if(isset($_POST['fieldValue']){} and keeping it to direct variable checking: <?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"]; if ($name == ''){ echo "You MUST fill in the Name field!"; include "yourFormPage.html"; } elseif ($number == ''){ echo "You MUST fill in the Number field!"; include "yourFormPage.html"; } elseif ($email == ''){ echo "You MUST fill in the E-Mail field!"; include "yourFormPage.html"; } else { rest of page code } you can expand on each of the if statements to include more failure criteria (such as no @ symbol in the email field or letters in the number one) but you're better to read up on those carefully to get the best out of implementing them. This should do what you need just now. Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052364 Share on other sites More sharing options...
JoeAM Posted May 3, 2010 Author Share Posted May 3, 2010 Thank you so much, Muddy_Funster. That's EXACTLY what I was trying to accomplish. You've really helped me out here. Really appreciate it. I've been trying to figure this out for days. Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052536 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 No worries, glad to help. Good luck Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052539 Share on other sites More sharing options...
abdfahim Posted May 3, 2010 Share Posted May 3, 2010 just 1 quick thought, isn't it better to have that done through Javascript so that form doesn't need to be submitted for validation, rather it validate without submitting the value? Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052548 Share on other sites More sharing options...
Muddy_Funster Posted May 3, 2010 Share Posted May 3, 2010 Would be lighter on the server, and leave information in the fields that have been filled in, but OP already said that he/she doesn't want to use JS. Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052553 Share on other sites More sharing options...
abdfahim Posted May 3, 2010 Share Posted May 3, 2010 ohk, i miss that point .. sorry .. Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052555 Share on other sites More sharing options...
roopurt18 Posted May 3, 2010 Share Posted May 3, 2010 Check the form links in my signature if you're still having trouble. just 1 quick thought, isn't it better to have that done through Javascript so that form doesn't need to be submitted for validation, rather it validate without submitting the value? And when the user is using NoScript, what then? Quote Link to comment https://forums.phpfreaks.com/topic/200543-prevent-a-form-from-processing-if-required-fields-arent-completed/#findComment-1052557 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.