Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Make Array From Checkbox

Tags:

php

I wan't to make an array from checkbox.

I've use array($_POST[test],$_POST[test1],$_POST[test2]) It works, but if once of array is NULL the array also NULL

So what I wan't is if once of array is NULL, it's not in array

Like this

$_POST['test']=NULL;
$_POST['test1']="ARAAY1";
$_POST['test2']="ARRAY2";

and it will be array($_POST[test1],$_POST[test2])

like image 734
user1742924 Avatar asked Jul 14 '26 16:07

user1742924


2 Answers

try something like

<input type="checkbox" name="options[]" value="one"/> one<br/>
<input type="checkbox" name="options[]" value="tow"/> tow<br/>
<input type="checkbox" name="options[]" value="three"/> three<br/>



$checked = $_POST['options'];
for($i=0; $i < count($checked); $i++){
    echo "Selected " . $checked[$i] . "<br/>";
}
like image 98
NullPoiиteя Avatar answered Jul 16 '26 07:07

NullPoiиteя


You can use is_null() to check if a value is NULL or isset() to check that it is set and not NULL. Then you can append that variable to the array if and only if it isn't NULL.

Even better do what @NullPointer has in their answer where you set up the form so that the checkbox values come in as an array to begin with.

like image 29
Trott Avatar answered Jul 16 '26 06:07

Trott



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!