eldan88 Posted September 16, 2013 Share Posted September 16, 2013 Hey guys. I am trying to create a simple form validation. But everytime i submit the empty form I get the following error: "Warning: Illegal offset type in isset or empty in ....../index.php on line 66Form Fields are empty" Below is my form and validation code. Any help would be highly appreciated. Validation <?php $form_fields = array('name','restaurant_name','state','phone_number'); if($_POST['submit']) { if(empty($_POST[$form_fields])) { echo "Form Fields are empty";} } ?> Form: <form action="index.php" method="post"> <input type="text" name="name" placeholder="Name" /> <input type="text" name="restaurant_name" placeholder="Restaurant Name" /> <input type="text" name="state" placeholder="State" /> <input type="text" name="phone_number" placeholder="Phone Number" /> <input type="submit" name="submit" value="Request a Demo" /> <input type="hidden" name="method" value="POST" /> </form> Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/ Share on other sites More sharing options...
drisate Posted September 16, 2013 Share Posted September 16, 2013 Tryif (!isset($_POST[$form_fields]){[...]} Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/#findComment-1449756 Share on other sites More sharing options...
AbraCadaver Posted September 16, 2013 Share Posted September 16, 2013 $form_fields is an array. You can't just stick it anywhere. I might do: if(count($_POST) != count(array_filter($_POST))) { //form fields are empty } Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/#findComment-1449757 Share on other sites More sharing options...
drisate Posted September 16, 2013 Share Posted September 16, 2013 Just noticed i missed a ) lol if (!isset($_POST[$form_fields])){[...]} Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/#findComment-1449762 Share on other sites More sharing options...
eldan88 Posted September 16, 2013 Author Share Posted September 16, 2013 $form_fields is an array. You can't just stick it anywhere. I might do: if(count($_POST) != count(array_filter($_POST))) { //form fields are empty } That is a create way of validating all the fields! I actually wanted the code to tell the user specifically what input fields where empty. I just put a generic echo statemenet to see if the condition worked. I actually played around with it a little bit and solved it with a foreach loop. Thank you for your help! $form_fields = array('name','restaurant_name','state','phone_number'); if($_POST['submit']) { foreach($form_fields as $fields) if(empty($_POST[$fields])) { echo "Form Fields are empty";} } Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/#findComment-1449769 Share on other sites More sharing options...
eldan88 Posted September 16, 2013 Author Share Posted September 16, 2013 @dristate. Thanks for your suggestion but it won't work. You need process through the array values. Link to comment https://forums.phpfreaks.com/topic/282203-need-help-with-form-validation/#findComment-1449770 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.