Jump to content

Need help with Form Validation


eldan88
Go to solution Solved by 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
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";}
    } 
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.