Jump to content

pre-checking a checkbox from array values when validating a form


cparekh

Recommended Posts

Hi,

 

I'm setting server-side validation for a form and would like to pre-check any checkboxes that have been checked by the user when I call the form again.

 

I'm using in_array to check if a value is present and this works when I call it like this:

//ERROR CHECKING for array
	echo count($location);
	if (in_array("Africa", $location)) 
		{
    			echo "Africa - yes<br/>";
		}
	if (in_array("Europe", $location)) 
		{
    			echo "Europe - yes<br/>";
		}

 

...however I get this error

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\xampp\htdocs\profile\index.php on line 294

> Africa

when I call in_array like this in a conditional:

<input type='checkbox' name='geographic[]' value='Africa' <?php if(in_array("Africa", $location)) {echo "checked='checked'";} ?> >

 

Having searched for solutions to this error, it usually has to do with a missing ';' however everything looks ok as far as I can tell.

 

If someone has an idea why it's throwing up that error or knows of another way to pre-populate checkboxes from an array I'd greatly appreciate the help.

Link to comment
Share on other sites

This error has nothing to do with a missing semi-colon, it is telling you that your second argument to the in_array() function is not an array. Do a

<?php
echo '<pre>' . print_r($location,true) . '</pre>';
?>

to see what's in that variable.

 

 

Ken

Link to comment
Share on other sites

Hi Ken,

 

this is the error checking I've done, now including what you've suggested:

 

//ERROR CHECKING for array
	echo count($location);

                if (in_array("Africa", $location)) 
		{
    			echo "Africa - yes<br/>";
		}
	if (in_array("Europe", $location)) 
		{
    			echo "Europe - yes<br/>";
		}

	echo '<pre>' . print_r($location,true) . '</pre><br/>';

 

(note the in_array check for 'Africa' & 'Europe') and the result is...

2Africa - yes
Europe - yes

Array
(
    [0] => Africa
    [1] => Europe
)

Link to comment
Share on other sites

Change

<input type='checkbox' name='geographic[]' value='Africa' <?php if(in_array("Africa", $location)) {echo "checked='checked'";} ?> >

to

<input type='checkbox' name='geographic[]' value='Africa' <?php if(is_array($location) && in_array("Africa", $location)) {echo "checked='checked'";} ?> >

 

This will check if $location is an array first.

 

Ken

Link to comment
Share on other sites

Cep,

 

var_dump($location); output the following (including other error checking as above)

2Africa - yes
Europe - yes

Array
(
    [0] => Africa
    [1] => Europe
)


array(2) { [0]=> string(6) "Africa" [1]=> string(6) "Europe" }

 

 

Link to comment
Share on other sites

You're code produced the error when it was not an array. Adding the check makes sure it's an array before checking whether the value is in the array.

 

Since you didn't post the rest of your code, I'm assuming that you go the error the first time through the code, before the form had been filled in and submitted.

 

Ken

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.