atrum Posted March 1, 2009 Share Posted March 1, 2009 Does any one know a way to validate that all fields are filled out in a form with out writing an if else statement for each field? Here is kinda what I would like. On my form, I have 10 fields. I want to have the form validate that all fields are not equal to nothing before it submits the data. I think I may have to put the fields into an array, but I am not to savy on php yet. any help any one can provide will be appreciated. Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/ Share on other sites More sharing options...
Stephen68 Posted March 1, 2009 Share Posted March 1, 2009 would think that it might be better to use JS to do this then PHP. I guess you could loop through all the form fields and see if they are empty and show an error or redirect of something. foreach ($_POST as $field=>$value) {//Start foreach loop if(empty($value)) { //The field has no value so do something here header('Location: http://www.example.com/'); } }// end of foreach loop Or something like that, you could even pass the field that was empty back to the page through the URL in the $_GET[]. Hope this helps in some way Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-773848 Share on other sites More sharing options...
MatthewJ Posted March 1, 2009 Share Posted March 1, 2009 You should use both javascript and php... javascript is client side only and will not validate your data if javascript support is disabled in the browser. Check with javascript before the form is allowed to submit, then check with php by looping through the $_POST array. Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-773867 Share on other sites More sharing options...
atrum Posted March 1, 2009 Author Share Posted March 1, 2009 Thank you for the help. Yeah the reason I don't want to use much java is because of the very reason that I don't want to risk the java validation not working, and thus alowing people to put just anything in the database. I also need to setup someway to enforce a password requirements system. For example. Password must contain 1 upper case, 1 lower, 1 number, and 8 characters. Does any one have any clue on things like that Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-773911 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi Javascript is useful to save some load on your server and save the user having to wait for the server to validate the form when there are obvious errors. But it is pretty much useless to prevent anyone maliciously entering dodgy data. Once it is into php you may as well fully validate it, checking each individual field. If you are feeling a bit masochistic then you can use Ajax to validate the form fields as they are entered. With a bit of care you can include the same php to validate the fields both via Ajax and via your main script. Can work well. All the best Keith Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-776226 Share on other sites More sharing options...
Yesideez Posted March 4, 2009 Share Posted March 4, 2009 You could prefix the names of form data with spmething specific and universal and run a loop... foreach ($_POST as $key => $val) { if (substr($key,0,4)=='frm_') { switch ($key) { case 'frm_name': $strName=(strlen($_POST['frm_name'])>0 ? $_POST['frm_name'] : false); break; case 'frm_age': $intAge=(intval($_POST['frm_age'])>0 ? intval($_POST['frm_age']) : false); break; } } } That way you can check each variable at the end. If $intAge or $strName is false the data was invalid. Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-776233 Share on other sites More sharing options...
atrum Posted March 4, 2009 Author Share Posted March 4, 2009 hmmm, would you mind explaining the code you gave me? I am a bit of a novice, and I am having a hard time understanding what the code is doing. Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-776770 Share on other sites More sharing options...
kickstart Posted March 4, 2009 Share Posted March 4, 2009 Hi That code is looping through all the return fields when the form is submitted, and if the name starts frm_ then it uses a case statement to decide which field it is and so how to validate it. All the best Keith Link to comment https://forums.phpfreaks.com/topic/147431-php-form-validation/#findComment-776817 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.