Jump to content

Need help with Form Validation


eldan88

Recommended Posts

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 66
Form 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

 

$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";}
    } 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.