oraya Posted October 14, 2012 Share Posted October 14, 2012 (edited) At present I have if statements that check that form fields aren't left empty, The onese that can't be left empty are: Title First Name Last Name Email Subject Comments Captcha (a simply random number question I wrote) Then depending if the check box for "Do you wish us to call you back?" is checked, then Phone must not be left empty I will need to add some other security checks so that first and last name, can only have letters and perhaps one hyphen, a check for the email and the phone can only be numbers of a certain length ect, but I'm wondering if there is an easier way to out put the errors? Or indeed if there is an easier way to cover the whole of this checking with an array and foreach? // Check if the form has been submitted: if ( isset($_POST['submitted']) ) { $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['title'])) { $problem = TRUE; $err1 = '<li>Title is required!</li>'; $errdisplay1 = 'error'; } if (empty($_POST['first_name'])) { $problem = TRUE; $err2 = '<li>First name is required!</li>'; $errdisplay2 = 'error'; } if (empty($_POST['last_name'])) { $problem = TRUE; $err3 = '<li>Last name is required!</li>'; $errdisplay3 = 'error'; } if (empty($_POST['email'])) { $problem = TRUE; $err4 = '<li>Email is required!</li>'; $errdisplay4 = 'error'; } if(isset($_POST['call']) && $_POST['phone'] == ''){ $problem = TRUE; $err5 = '<li>Please provide a valid phone number!</li>'; $errdisplay5 = 'error'; } if (empty($_POST['subject'])) { $problem = TRUE; $err6 = '<li>Subject is required!</li>'; $errdisplay6 = 'error'; } if (empty($_POST['comments'])) { $problem = TRUE; $err7 = '<li>Message is required!</li>'; $errdisplay7 = 'error'; } /*Rest of form process, such as if else statements to send mail to different recipients depending on the subject selected etc. Haven't included as not relivant.*/ } /* FORM */ I'm really new to Php so I'd be greatful of some feed back and maybe a pointer in the right direction so that I can do a little more research. Kind regards Oraya EDIT Oh I should point out the first $err# is the error message that is displayed, and the second $errdisplay# is what is placed into the class tag so that my css can outline the field in red. Hence the reason I'm hoping to find a way of out putting only two error messages for all, the first perhaps changing the name with an array! Edited October 14, 2012 by oraya Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/ Share on other sites More sharing options...
oraya Posted October 14, 2012 Author Share Posted October 14, 2012 (edited) Sorry just realised left off part of the error check // Check if the form has been submitted: if ( isset($_POST['submitted']) ) { $problem = FALSE; // No problems so far. // Check for each value... if (empty($_POST['title'])) { $problem = TRUE; $err1 = '<li>Title is required!</li>'; $errdisplay1 = 'error'; } if (empty($_POST['first_name'])) { $problem = TRUE; $err2 = '<li>First name is required!</li>'; $errdisplay2 = 'error'; } if (empty($_POST['last_name'])) { $problem = TRUE; $err3 = '<li>Last name is required!</li>'; $errdisplay3 = 'error'; } if (empty($_POST['email'])) { $problem = TRUE; $err4 = '<li>Email is required!</li>'; $errdisplay4 = 'error'; } if(isset($_POST['call']) && $_POST['phone'] == ''){ $problem = TRUE; $err5 = '<li>Please provide a valid phone number!</li>'; $errdisplay5 = 'error'; } if (empty($_POST['subject'])) { $problem = TRUE; $err6 = '<li>Subject is required!</li>'; $errdisplay6 = 'error'; } if (empty($_POST['comments'])) { $problem = TRUE; $err7 = '<li>Message is required!</li>'; $errdisplay7 = 'error'; } ///SETTING THE CAPTCHA VARIALBES TO CHECK $num1 = $_POST['num1']; $num2 = $_POST['num2']; $num3 = $_POST['num3']; $answer = $_POST['answer']; $add = $num1+$num2; $total = $add-$num3; if(empty($_POST['answer'])){ $problem = TRUE; $err8 = '<li>Please complete the Captcha!</li>'; $errdisplay8 = 'error'; }elseif($_POST['answer'] != $total){ $problem = TRUE; $err9 = '<li> Captcha was incorrect please try again!</li>'; $errdisplay8 = 'error'; } if (!$problem) { // IF THERE WEREN'T ANY PROBLEMS THEN WE PROCESS THE FORM. /*Rest of form process, such as if else statements to send mail to different recipients depending on the subject selected etc. Haven't included as not relivant.*/ } } /* FORM */ Edited October 14, 2012 by oraya Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/#findComment-1385119 Share on other sites More sharing options...
thara Posted October 14, 2012 Share Posted October 14, 2012 where did you use foreach loop? Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/#findComment-1385123 Share on other sites More sharing options...
White_Lily Posted October 14, 2012 Share Posted October 14, 2012 Shes asking if the errors can be processed through a forech() statement Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/#findComment-1385127 Share on other sites More sharing options...
PFMaBiSmAd Posted October 14, 2012 Share Posted October 14, 2012 You can use two arrays to simplify the creation/validation of form fields and the storage/processing of errors. The first array 'defines' the form fields and contains information about them, such as if they are required,... (Once you have this array, you can also use it to dynamically produce the form.) The second array holds any validation errors. An empty array indicates no errors, which would eliminate the need for the $problem variable in your code. <?php // define the form fields. the array index is the form field name. add other elements to the array for any form field to address your field creation/validation needs, such as the regex pattern to use for validation or a ctype function name to call $form_fields['title'] = array('legend'=>'Title','required'=>1); $form_fields['first_name'] = array('legend'=>'First Name','required'=>1); // form processing code if(isset($_POST['submit'])){ $errors = array(); // an empty array = no errors foreach($form_fields as $index=>$info){ if($info['required'] && empty($_POST[$index])){ $errors[$index] = array('err'=>"<li>{$info['legend']} is required!</li>", 'errdisplay'=>'error'); } } } // check if any validation errors if(!empty($errors)){ // for demo purposes, take a look at what the $errors array contains - echo '<pre>',print_r($errors,true),'</pre>'; } Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/#findComment-1385131 Share on other sites More sharing options...
oraya Posted October 14, 2012 Author Share Posted October 14, 2012 Wonderful, thank you PFMaBiSmAd, I'll have a look at this later this evening, once I have finished cooking dinner. And I will no doubt be back to ask questions. Hope that is ok? As I am teaching myself Php & Mysql, usually for the few hours each night I get some me time and like to know and understand exactly how things work and what it's doing. I'm very grateful for the reply. Kindest wishes, Oraya Quote Link to comment https://forums.phpfreaks.com/topic/269447-would-a-foreach-be-better-here/#findComment-1385153 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.