sawade Posted September 2, 2009 Share Posted September 2, 2009 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. Quote Link to comment https://forums.phpfreaks.com/topic/172899-solved-checkbox-array/ Share on other sites More sharing options...
bundyxc Posted September 2, 2009 Share Posted September 2, 2009 I could be wrong, but wouldn't $history return boolean false if the checkbox was empty and $_POST['history'] was not set? Quote Link to comment https://forums.phpfreaks.com/topic/172899-solved-checkbox-array/#findComment-911265 Share on other sites More sharing options...
sawade Posted September 7, 2009 Author Share Posted September 7, 2009 Right. Which is why I used isset Quote Link to comment https://forums.phpfreaks.com/topic/172899-solved-checkbox-array/#findComment-914232 Share on other sites More sharing options...
Psycho Posted September 7, 2009 Share Posted September 7, 2009 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']) : ''; Quote Link to comment https://forums.phpfreaks.com/topic/172899-solved-checkbox-array/#findComment-914254 Share on other sites More sharing options...
sawade Posted September 10, 2009 Author Share Posted September 10, 2009 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!! Quote Link to comment https://forums.phpfreaks.com/topic/172899-solved-checkbox-array/#findComment-916055 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.