Been struggling with this some time now and is probably something simple...
I keep getting the following error whilst trying to submit a contact form
Warning: implode() [function.implode]: Invalid arguments passed in .... on line 240.
HTML CODE
<input type="checkbox" name="socialmedia[]" value="Facebook">Facebook<br>
<input type="checkbox" name="socialmedia[]" value="Twitter">Twitter<br>
<input type="checkbox" name="socialmedia[]" value="YouTube">YouTube<br>
<input type="checkbox" name="socialmedia[]" value="Flickr">Flickr<br>
<input type="checkbox" name="socialmedia[]" value="Vimeo">Vimeo<br>
PHP CODE
$socialmedia = array();
$socialmedia = implode(",",$_POST['socialmedia']);
Any Ideas anyone?
Here you go:
<form method="post">
<input type="checkbox" name="socialmedia[]" value="Facebook">Facebook<br>
<input type="checkbox" name="socialmedia[]" value="Twitter">Twitter<br>
<input type="checkbox" name="socialmedia[]" value="YouTube">YouTube<br>
<input type="checkbox" name="socialmedia[]" value="Flickr">Flickr<br>
<input type="checkbox" name="socialmedia[]" value="Vimeo">Vimeo<br>
<input type="submit">
</form>
<?
$socialmedia = array();
if(isset($_POST['socialmedia'])) $socialmedia = implode(",",$_POST['socialmedia']);
print_r($socialmedia);
?>
only change is additional checking if(isset($_POST['socialmedia']))
before imploding the array
I've seen this before when the user didn't check any boxes. The reason you get a warning is because $_POST['socialmedia']
doesn't actually exist if no checkboxes are checked. You are effectively calling implode(",", NULL);
To get the selected checkboxes in a string:
$checkboxes = !empty($_POST['socialmedia']) ? $_POST['socialMedia'] : array();
$socialmedia = implode(',' $checkboxes);
It seems like you might want them in an array. It's hard to tell from your question. If you want them in an array you can just do:
$socialmedia = !empty($_POST['socialmedia']) ? $_POST['socialMedia'] : array();
$socialmedia
will then be an empty array if no checkboxes were checked or will contain the values of the checkboxes.