gum1982 Posted November 11, 2009 Share Posted November 11, 2009 Hello i really need help i need to validate a form but i want one set of valdaition to be confirmed before it even runs the next bit of code. here is my code. if(isset($_POST['submit'])){ $error = ''; // set error variable to blank if(trim($_POST[firstname]) == '') { $error.="Please enter you firstname<br />"; } if(trim($_POST[lastname]) == '') { $error.="Please enter you lastname<br />"; } if(trim($_POST[email])==''){ $error.="An email address is required!<br />"; } else { if(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { $error.="The e-mail you entered was not in the proper format!"; } } if(trim($_POST[custom_CompanyName]) == '') { $error.="Please enter your company name<br />"; } if(trim($_POST[custom_CompanyAddress1]) == '') { $error.="Please enter your company address<br />"; } if(trim($_POST[custom_Postcode]) == '') { $error.="Please enter your postcode<br />"; }else{ if(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) { $error="Please enter a valid postcode!"; } } if(trim($_POST[custom_Telephone]) == '') { $error.="Please enter your telephone number<br />"; }else{ if(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) { $error="Please enter only numbers!"; } } if(!isset($_POST['check'])) { $error = "The checkbox isn't checked"; } // I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE. if(isset($_POST[code])==''){ header('Location: [url=https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&[email protected]&item_name=Busines+Health+Check&hosted_button_id=1078955&firstname=']https://www.sandbox.paypal.com/cgi-bin/webscr?cmd=_s-xclick&[email protected]&item_name=Busines+Health+Check&hosted_button_id=1078955&firstname='[/url]); }else{ header('location: [url=http://example.com]http://example.com[/url]); } } Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/ Share on other sites More sharing options...
gum1982 Posted November 11, 2009 Author Share Posted November 11, 2009 Basically what i am asking is how do you run code in succession so one block of code will only run once another one has been confirmed. what statement shall i use. I am still a newbie with php and my knowledge is limited. Help Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955524 Share on other sites More sharing options...
runnerjp Posted November 11, 2009 Share Posted November 11, 2009 I would have thought if(trim($_POST[firstname]) != '') { ... carry on this being not equal != Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955526 Share on other sites More sharing options...
gum1982 Posted November 11, 2009 Author Share Posted November 11, 2009 Thanks for the reply i tried that with no luck i guess what i am trying to do is validate the form in two sections so the second part only run's after the first part has validated successfully. Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955531 Share on other sites More sharing options...
runnerjp Posted November 11, 2009 Share Posted November 11, 2009 Funny enough thats what im trying today as im sure you have seen im my last post Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955534 Share on other sites More sharing options...
isedeasy Posted November 11, 2009 Share Posted November 11, 2009 You could use elseif() or check to see if $error == '' something like this (not tested) <?php if(isset($_POST['submit'])){ $error = ''; // set error variable to blank if(trim($_POST[firstname]) == '') { $error.="Please enter you firstname<br />"; } elseif(trim($_POST[lastname]) == '') { $error.="Please enter you lastname<br />"; } elseif(trim($_POST[email])==''){ $error.="An email address is required!<br />"; } elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { $error.="The e-mail you entered was not in the proper format!"; } elseif(trim($_POST[custom_CompanyName]) == '') { $error.="Please enter your company name<br />"; } elseif(trim($_POST[custom_CompanyAddress1]) == '') { $error.="Please enter your company address<br />"; } elseif(trim($_POST[custom_Postcode]) == '') { $error.="Please enter your postcode<br />"; } elseif(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) { $error="Please enter a valid postcode!"; } elseif(trim($_POST[custom_Telephone]) == '') { $error.="Please enter your telephone number<br />"; } elseif(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) { $error="Please enter only numbers!"; } elseif(!isset($_POST['check'])) { $error = "The checkbox isn't checked"; } else { // I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE. if(isset($_POST[code])==''){ header('Location: http://example.com'); }else{ header('location: http://example.com'); } } } ?> And as this means you will only have one error message up at a time you could change the code to the following (again, not tested). <?php if(isset($_POST['submit'])){ if(trim($_POST[firstname]) == '') { $error = "Please enter you firstname"; } elseif(trim($_POST[lastname]) == '') { $error = "Please enter you lastname"; } elseif(trim($_POST[email])==''){ $error = "An email address is required!"; } elseif(!eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST[email])) { $error = "The e-mail you entered was not in the proper format!"; } elseif(trim($_POST[custom_CompanyName]) == '') { $error = "Please enter your company name"; } elseif(trim($_POST[custom_CompanyAddress1]) == '') { $error = "Please enter your company address"; } elseif(trim($_POST[custom_Postcode]) == '') { $error = "Please enter your postcode"; } elseif(!eregi("^([Gg][ii][Rr] 0[Aa]{2})|((([A-Za-z][0-9]{1,2})|(([A-Za-z][A-Ha-hJ-Yj-y][0-9]{1,2})|(([A-Za-z][0-9][A-Za-z])|([A-Za-z][A-Ha-hJ-Yj-y][0-9]?[A-Za-z])))) [0-9][A-Za-z]{2})$", $_POST[custom_Postcode])) { $error = "Please enter a valid postcode!"; } elseif(trim($_POST[custom_Telephone]) == '') { $error = "Please enter your telephone number"; } elseif(!eregi("^[0-9\ ]+$", $_POST[custom_Telephone])) { $error = "Please enter only numbers!"; } elseif(!isset($_POST['check'])) { $error = "The checkbox isn't checked"; } if(!isset($error)) { // I WANT THIS BIT OF CODE TO RUN BUT ONLY AFTER THE REST HAS VALIDATED, SORRY TO JUST DROP LINES OFF CODE IN I'VE TRIED SO MANY DIFFERENT METHODS AND I AM GETTING FRUSTRATED, CURRENTLY IT JUST INSTANTLY REDIRECTS AFTER SUBMIT TO PAYPAL. BUT I ONLY WANT THIS TO HAPPEN AFTER THE REST OFF THE FIELDS HAVE VALIDATED CAN SOMEONE TELL ME THE BEST METHOD TO DO THIS PLEASE. if(isset($_POST[code])==''){ header('Location: http://example.com'); }else{ header('location: http://example.com'); } } } ?> Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955539 Share on other sites More sharing options...
gum1982 Posted November 11, 2009 Author Share Posted November 11, 2009 Brilliant thanks for making the effort to reply this looks like what i need so it will run each bit off code after the first has been validated. thanks Link to comment https://forums.phpfreaks.com/topic/181099-solved-validation/#findComment-955550 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.