I have 1000 checkboxes of object fields and I want to create an Array from all the fields that I check after submitting using PHP can you tell me how to do it?
well if you name your checkbox like this, you already have an array:
<input name="mycheckbox[]" value="1"> hello1
<input name="mycheckbox[]" value="2"> hello2
<input name="mycheckbox[]" value="3"> hello3
in PHP you will get:
print_t($_REQUEST['mycheckbox']);
/*
[
0 => '1',
0 => '2',
0 => '3'
]
*/
<input type="checkbox" name="somename[]" value="somevalue"> if you need an array with numerical indices
or
<input type="checkbox" name="someobject[property]" value="somevalue"> if you are looking for an associative array
Related
I'm trying to insert multiple values using radio, here is my example:
<input type="radio" name="toppingPrice[]" value="<?= $topping['name'];?>-<?= $topping['price'];?>">
this one work if I insert single input, but if I want to insert more than one for example:
Size: small, medium, large <- name="toppingPrice[]" for all input values
Cheese: yes, no <- name="toppingPrice[]" for all input values
Level: spicy, normal <- name="toppingPrice[]" for all input values
this will not work because it will merge into 1 group so if I have to choose only one of all toppings.
my original code looks like:
foreach ($toppingPrice as $key) {
list($toppingName,$toppingNameEn, $toppingPrice) = explode("-",$key,3);
$tName[] = $toppingName;
$tNameEn[] = $toppingNameEn;
$tPrice += $toppingPrice;
}
$tn = implode(",", $tName);
$tn_en = implode(",", $tNameEn);
$price = $price + $tPrice;
Html:
<input type="checkbox" name="toppingPrice[]" id="<?= $topping[0];?>" value="<?= $topping['name'];?>-<?= $topping['name_en'];?>-<?= $topping['price'];?>" <? if($topping['price'] < 1){echo "checked";}?>>
I hope I delivered the question in the right way
please give me any idea or solution for fix this issue
You should use name attribute as shown in below code.
<?php
echo "<pre>";
print_r($_POST);
echo "</pre>";
?>
<form name="test" method="post">
<input type="radio" name="toppingPrice.size[]" value="1"> 1
<input type="radio" name="toppingPrice.size[]" value="2"> 2
<input type="radio" name="toppingPrice.size[]" value="3"> 3
<br>
<input type="radio" name="toppingPrice.toping[]" value="4"> 4
<input type="radio" name="toppingPrice.toping[]" value="5"> 5
<input type="radio" name="toppingPrice.toping[]" value="6"> 6
<br>
<input type="radio" name="toppingPrice.level[]" value="7"> 7
<input type="radio" name="toppingPrice.level[]" value="8"> 8
<input type="radio" name="toppingPrice.level[]" value="9"> 9
<button type="submit" value="save" /> Save
</form>
This will be your $_POST after form submit
Array
(
[toppingPrice_size] => Array
(
[0] => 1
)
[toppingPrice_toping] => Array
(
[0] => 5
)
[toppingPrice_level] => Array
(
[0] => 9
)
)
I am posting checkboxes from an HTML form and am having a weird problem. The first is that I have a default checked and disabled box at the top of the form, but it doesn not get included in the POST data. The second is that If I don't check something, the entire array is left out.
How can I get it to 1) include my default box and 2) POST an empty array if none are selected?
Here's the code:
<form action="file.php" method="POST">
<label><input type="checkbox" name="options[]" value="username" checked disabled> Username</label><br>
<label><input type="checkbox" name="options[]" value="title"> Title</label>
<label><input type="checkbox" name="options[]" value="first"> First Name</label>
<label><input type="checkbox" name="options[]" value="last"> Last Name</label><br>
<label><input type="checkbox" name="options[]" value="address"> Address</label>
<label><input type="checkbox" name="options[]" value="city"> City</label>
<label><input type="checkbox" name="options[]" value="state"> State</label>
<label><input type="checkbox" name="options[]" value="zip"> ZIP</label><br>
<label><input type="checkbox" name="options[]" value="email"> Email</label>
<label><input type="checkbox" name="options[]" value="phone"> Phone</label><br>
<input type="submit" value="submit">
</form>
file.php
<?php var_dump($_POST)
This is a part of standard HTML (i.e. not a browser thing). By definition, unchecked boxes are never successful. Consider a different data structure, or adding something like
if(isset($_POST['options'])) {
//work with options here
}
If that won't work, you can always include a hidden element, that will at least get you the value in $_POST
<input type="hidden" name="options[]" value="NA">
You could also do something like this without changing your HTML at all. Simply, create a list of all possible checkbox values and compare with those posted. As far as username is concerned, since it will always be there, you could just add it manually to the $_POST array.
// Auto insert username to $_POST array (because it's always there by default)
$_POST['options'][] = 'username';
// Create array of all possible checkbox values
$boxes = array('username','title','first','last','address','city','state','zip','email','phone');
// Compare $_POST array to list of possible checkboxes
// and create manual post array
$post_array = array();
foreach ($boxes as $box) {
$post_array[$box] = in_array($box, $_POST['options']) ? 'checked' : 'NOT checked';
}
The output will be an array, $post_array, which will contain something like:
Array
(
[username] => checked
[title] => checked
[first] => NOT checked
[last] => NOT checked
[address] => checked
[city] => checked
[state] => NOT checked
[zip] => NOT checked
[email] => NOT checked
[phone] => checked
)
I have two arrays. One for input boxes and other for checkbox.
inputbox[] checkbox[]
inputbox[] checkbox[]
.
.
.
.
submit button
When I fill check box1 and fill the value in input box1 and try to submit.
Foreach fails because it pass all the indexes of input boxes but only passes checked checkbox.
foreach(array_combine($checkbox, $inputbox) as $check => $input)
Please tell me what can i do?
if you have a control over the HTML form you can make the form in following manner
<input type="text" name="name[1]" />
<input type="checkbox" name="check[1]" />
<input type="text" name="name[2]" />
<input type="checkbox" name="check[2]" />
<input type="text" name="name[3]" />
<input type="checkbox" name="check[3]" />
<input type="text" name="name[4]" />
<input type="checkbox" name="check[4]" />
in that case you will get the post array in the following manner
Array
(
[name] => Array
(
[1] => Swapnil
[2] =>
[3] => Sarwe
[4] => Swapnil Sarwe
)
[check] => Array
(
[1] => on
[3] => on
)
)
Now you can loop over the name(input box) and then check isset for the isset($_POST['check'][$key]) and set the default value
Iterate over textboxes (which are guaranteed to all be present), then fetch the corresponding checkbox (probably by ID, if you have some kind of ID correspondence between them - which you should).
I have a form with many checkboxes.
ex.
...
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
<input name="dodatkowe[]" type="checkbox" value="1" />
...
I want to have all the checkboxes in the array. Array 'dodatkowe'.
When i checked all checkboxes have:
Array ( [0] => 1 [1] => 1 [2] => 1 )
but when i checked example only second I have:
Array ( [0] => 1 )
I need that, when i check example second checkbox:
Array ( [0] => 0 [1] => 1 [2] => 0)
give them indexes so you can reference them specifically...
...
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
<input name="dodatkowe[3]" type="checkbox" value="1" />
...
Not sure why you feel you need to see the unchecked values, this can be assumed to be the inverse of the checked values.... Any attempt to do this is a hack, and is unnecessary.
If a checkbox isn't checked it won't include it's value into the parameters but the first step would be to give the checkboxes a unique id:
<input name="dodatkowe[0]" type="checkbox" value="1" />
<input name="dodatkowe[1]" type="checkbox" value="1" />
<input name="dodatkowe[2]" type="checkbox" value="1" />
Then you can use PHP to check is the value is there:
$maxfields = 3;
$selectboxes = $_REQUEST['dodatkowe'];
for($i = 0; $i < $maxfields; $i++)
if(!isset($selectboxes[$i])) $selectboxes[$i] = 0;
This will set all non existent fields to 0 and $selectboxes should contain the result you are looking for.
I have a huge form (for an internal CMS) that is comprised by several sections, some of them are optional some of them are compulsory. All is under an humungous form (it has to be like this, no ajax, no other ways :-( )
Since in a Dilbertesque way everything get changed every second I was wondering if there is any simple way of grouping $_POST data, I mean sending POST like this:
$_POST['form1']['datax']
or to retrieve data from server side easily, and by easily I mean withouth having to expressily declare:
$array1 = array($_POST['datax'],$_POST['datay'],...);
$array2 = array($_POST['dataalpha'],$_POST['dataomega'],...);
since there are around 60 fields.
I hope I was able to explain this well and as always thank you very much..
If you give your input elements array-like names, they arrive in the PHP $_POST (or $_GET) array as an array:
<input type="text" name="foo[]" value="a"/>
<input type="text" name="foo[]" value="b" />
<input type="text" name="foo[]" value="c" />
<input type="text" name="foo[bar]" value="d" />
<input type="text" name="foo[baz][]" value="e" />
<input type="text" name="foo[baz][]" value="f" />
Goes to:
print_r($_POST)
foo => array (
0 => a
1 => b
2 => c
bar => d
baz => array(
0 => e
1 => f
)
)
If you name your inputs properly, you can do that. Example:
<input type="text" name="textInput[]" />
<input type="text" name="textInput[]" />
That will populate an array in $_POST named textInput. That is:
$_POST['textInput'][0] == "whatever the first was set to be"
$_POST['textInput'][1] == "whatever the second was set to be"
Using square brackets after the input name will cause it to be grouped in PHP:
<input name="foo[]" type="text" value="1" />
<input name="foo[]" type="text" value="2" />
You can also make an associative array:
<input name="foo[bar]" type="text" />
I think multi-dimensional arrays would also work, but I'm not sure if I've actually tried it.
Edit: Here's the same thing answered in the PHP FAQ.
you can use your form fields like this:
<input type="text" name="form1['datax']"/>