Warning: in_array() - Wrong datatype for second argument

Hello,

I am trying to check that all my fields in my form have value when submited. So instead of using isset i am using in_array, but get an error (Wrong datatype for second argument)

can anyone help



$form = array('FirstName', 'LastName', 'EmailAddress', 'YourBorough');
	
	foreach($_POST as $key => $value){
		if(in_array($value, $from)){
				echo 'all fields filled in';						
		}

thanks

Is it because it says from rather than form? :stuck_out_tongue:

it is because whole algorithm is wrong.

ha lol thats true thanks for spotting that. On another note, i am trying to see if my posted form has all fields filled in, but it looks like in_array is not what i should use as it’s not checking if it set (like isset)? any ideas? i dont want to check each individual field if its set or not, but would to use a loop.


$invalidField = false;
foreach ($fields as $fieldName) {
    if (!isset($_POST[$fieldName]) || strlen($_POST[$fieldName]) == 0) {
        $invalidField = true;
        break;
    }
}

Alternatively, make an $invalidFields array and when a bad field is found, put the field name into the array. Now you can tell the user exactly which fields were missing.

ok well this seems to do it as well eventough it very primitive:


foreach($_POST as $key => $value){
				
		if(empty($value) || strstr("void", $value)){
				echo '123';						
		}

thanks very much dude your solution is better then the one i came up with :slight_smile:

Yes.
Such situation may never occur if ma201dq had used:

error_reporting(E_ALL);
ini_set('display_errors', true);

Or better:


error_reporting(-1);
ini_set('display_errors', 1);

-1 will catch all possible errors including E_STRICT, E_DEPRECATED etc. and those errors might go to E_ALL in php 6, so catch it now and your code will be compatible with future versions of php.