sjmiller Posted January 10, 2009 Share Posted January 10, 2009 So I have what could be an extensive form that requires users to fill out all form inputs. If even one of these inputs is missing, I need to have it return an error. I find it somewhat inconvenient to go through 20-some isset()'s or empty()'s in one if() statement. Is there any easy way to just count the $_POST array's variables that contain data without having to make an extensive one-by-one validation that each $_POST value has been filled out? Thanks ahead of time for your help! Quote Link to comment https://forums.phpfreaks.com/topic/140252-solved-did-the-user-fill-out-the-entire-form/ Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 If you're going to validate a form do it right. you could do a javascript validation before hand but you should always check incoming data.. // javascript form validator function validate(TheForm) { for (i in TheForm.childNodes) { if (TheForm.childNodes[i].value == '' || (''+TheForm.childNodes[i].value+'') == 'undefined') { alert("ERROR!!"); return false; } } // if script reaches here without returning false than its obviously true.. return true; } and then <form action="whatever.php" method="POST" onsubmit="validate(this)"> <input... ... ... </form> Quote Link to comment https://forums.phpfreaks.com/topic/140252-solved-did-the-user-fill-out-the-entire-form/#findComment-733860 Share on other sites More sharing options...
RussellReal Posted January 10, 2009 Share Posted January 10, 2009 sorry I hit submit too fast! and if I modify that 1 you won't read the php solution; <?php foreach ($_POST as $k => $v) { // $k will be the field name, $v will be the value of the field if ($v == '') die("error"); } // if script reaches here without die() than assume good values ?> Quote Link to comment https://forums.phpfreaks.com/topic/140252-solved-did-the-user-fill-out-the-entire-form/#findComment-733862 Share on other sites More sharing options...
sjmiller Posted January 10, 2009 Author Share Posted January 10, 2009 Oh wow, was unaware that I was able to do an 'as' turning the associative array into two separate variables! That's a neat trick! Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/140252-solved-did-the-user-fill-out-the-entire-form/#findComment-733876 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.