Jump to content

[SOLVED] Checkbox Array


sawade

Recommended Posts

I am getting a new error.  This warning showed up after installing Zend Optimizer for PHP 5.2.x.

 

PHP Warning:  implode(): Invalid arguments passed

 

Here is my code:

$history = isset($_POST['history']) ? $_POST['history'] : '';
$history_list = implode(", ", $history);

<tr>
    		<td>
            	<input type="checkbox" name="history[]" id="history22" value="Scarlet fever" 
                	<?php if (isset ($_POST['history']) && in_array ("Scarlet fever", $_POST['history'])) echo ' checked="checked"'; ?> />
                	<label for="history22">Scarlet fever</label>
            </td>
    		<td>
            	<input type="checkbox" name="history[]" id="history23" value="Psychiatric treatment" 
                	<?php if (isset ($_POST['history']) && in_array ("Psychiatric treatment", $_POST['history'])) echo ' checked="checked"'; ?> />
                	<label for="history23">Psychiatric treatment</label>
            </td>
  		</tr> etc.etc.

 

This error only occurs when there are no values in the array.  Once a checkbox is checked the warning goes away.

Link to comment
Share on other sites

If a checkbox is not checked then nothing is submitted for the field in the POST data - not a null value, an empty string, etc. The POST array will not include an index for that field.

 

So, using isset() is the right approach. But, the implementation needs to be tweaked. When the checkbox isn't checked you are setting the variable $history to an empty string. Then you attempt to perform an explode() on that, thus the error. So, if my assumptions are correct IF the checkbox(es) are checked you want a string of the valuess that are comma separated, otehrwise you want an empty string.

 

$history = (isset($_POST['history']) && is_array($_POST['history'])) ? implode(', ', $_POST['history']) : '';

Link to comment
Share on other sites

If a checkbox is not checked then nothing is submitted for the field in the POST data - not a null value, an empty string, etc. The POST array will not include an index for that field.

 

So, using isset() is the right approach. But, the implementation needs to be tweaked. When the checkbox isn't checked you are setting the variable $history to an empty string. Then you attempt to perform an explode() on that, thus the error. So, if my assumptions are correct IF the checkbox(es) are checked you want a string of the valuess that are comma separated, otehrwise you want an empty string.

 

$history = (isset($_POST['history']) && is_array($_POST['history'])) ? implode(', ', $_POST['history']) : '';

 

This works perfectly.  Thank you!!

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.