cparekh Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/ Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 What is $location? Link to comment https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494730 Share on other sites More sharing options...
cparekh Posted March 18, 2008 Author Share Posted March 18, 2008 Hi Cep, I set $location = $_POST['geographic']; to hold the array when the user submits. Link to comment https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494734 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494735 Share on other sites More sharing options...
cparekh Posted March 18, 2008 Author Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494747 Share on other sites More sharing options...
Cep Posted March 18, 2008 Share Posted March 18, 2008 If you do var_dump($location); Do you get an array? Link to comment https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494752 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494753 Share on other sites More sharing options...
cparekh Posted March 18, 2008 Author Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494763 Share on other sites More sharing options...
cparekh Posted March 18, 2008 Author Share Posted March 18, 2008 Ken, tried that and it works! I'm not sure I get it though - if it is an array, why would in-array throw a wobbly? Thanks for that - phew! Link to comment https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494767 Share on other sites More sharing options...
kenrbnsn Posted March 18, 2008 Share Posted March 18, 2008 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 https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494842 Share on other sites More sharing options...
cparekh Posted March 18, 2008 Author Share Posted March 18, 2008 Ah yes, of course! No array built before submission. Thanks again Ken. Link to comment https://forums.phpfreaks.com/topic/96677-pre-checking-a-checkbox-from-array-values-when-validating-a-form/#findComment-494912 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.