html checkboxes with none selected does not POST - php

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
)

Related

Post values of a checkbox

I have this code on my form for the checkboxs:
<input type="hidden" name="option_desc[]" value="option 1"/>
<label><input type="checkbox" name="option_price[]" value="10" class="option_checkbox"/>option 1</label>
<input type="hidden" name="option_desc[]" value="option 2"/>
<label><input type="checkbox" name="option_price[]" value="20" class="option_checkbox"/>option 2</label>
<input type="hidden" name="option_desc[]" value="option 3"/>
<label><input type="checkbox" name="option_price[]" value="30" class="option_checkbox"/>option 3</label>
I'm trying to get the POST values of the checkbox that the user checked (for example, if he checked the second checkbox: "option 2" + "20") and store them:
$articleDetails['options'] = array();
$count = 0;
if(is_array($_POST['option_price'])){
foreach($_POST['option_price'] as $key => $value){
if($value){
$articleDetails['options'][$count]['option_price'] = $_POST['option_price'][$key];
$articleDetails['options'][$count]['option_desc'] = $_POST['option_desc'][$key];
$count++;
}
}
}
When the user check one of the checkbox, the appropriate 'option_price' is stored correctly, but the 'option_desc' is not the one that belongs to the CHECKED checkbox (for example: when the second checkbox is checked the values that I get are "20" (GOOD) and "option 1" (NOT GOOD).
What I'm doing wrong?
Thanks.
The problem you are having is that if checkboxes are not checked, the data won't get POSTed while your hidden inputs will always be posted. Frankly, I don't see what value your hidden inputs give you here. They tell you absolutely nothing about the posted data that you don't already know about on the server.
You should simply use a defined index in your array notation for the checkbox field, like this:
<input type="checkbox" name="option_price[1]" value="10" class="option_checkbox"/>option 1</label>
<input type="checkbox" name="option_price[2]" value="20" class="option_checkbox"/>option 2</label>
<input type="checkbox" name="option_price[3]" value="30" class="option_checkbox"/>option 3</label>
Note: I didn't use zero-indexed array here as I figured you might want to directly relate $_POST['option_price'][1] to "option 1".
To add some examples related to the discussion in comments below. Say for example you had some option_price checkboxes you want to output, and they come from some dynamic source. Your code to to generate the checkboxes might look something like this:
$price_options = array(
array(
'description' => 'eBook',
'value' => 10
),
array(
'description' => 'articles',
'value' => 20
),
// and so on
);
$count = count($price_options)
for($i = 1; $i<= $count; $i++) {
?>
<input type="checkbox" name="option_price[<?php echo $i; ?>]" value="<?php echo $price_options[$i]['value']; ?>" class="option_checkbox"/><?php echo $price_options[$i]['description']; ?></label>
<?php
} // end for
When POSTing you know that every $_POST['option_price'][x] would correspond to the item at $price_options[x].
You could simply iterate of $_POST['option_price'] to see which items are selected like this:
if(!empty($_POST['option_price']) {
foreach ($_POST['option_price'] as $index => $value) {
// verify value hasn't been tampered with
if ((int) $value === $price_options[$index]['value']) {
// set description
$description = $price_options[$index]['description'];
var_dump($description, $value);
}
}
}
I would suggest give names to input like
<input type=hidden name="option[1][desc]"/>
<input type=hidden name="option[1][price]"/>
Then I think it would be easy to run a forreach as well.

Determine unchecked boxes in a submitted form using PHP

I just cant figure this one out:
I have a list of items:
Front and Back of Title -
Drivers License -
Vehicle Insurance -
Proof of Residence -
Proof of Income -
4 References
And a form where people check off those items.
When they press submit, I have some code that gets the values they checked, and puts them in an array like this:
Array (
[0] => Front and Back of Title
[1] => Drivers License
[2] => Vehicle Insurance
[3] => Proof of Residence
[4] => Proof of Income
[5] => 4 References
)
So the array contains any values they checked..
Here is the relevant HTML:
<input type="checkbox" name="check_list[]" value="Full Title Loan Applicaiton">Full Title Loan Applicaiton <br />
<input type="checkbox" name="check_list[]" value="Front and Back of Title">Front and Back of Title<br />
<input type="checkbox" name="check_list[]" value="Drivers License">Drivers License<br />
<input type="checkbox" name="check_list[]" value="Vehicle Insurance">Vehicle Insurance<br />
<input type="checkbox" name="check_list[]" value="Proof of Residence">Proof of Residence<br />
<input type="checkbox" name="check_list[]" value="Proof of Income">Proof of Income<br />
How would derive what values they DID not check?
Check if the item is in the array. I'm not sure how your logic is set up, but I'm assigning a variable here:
$driversLicence = in_array('Drivers License', $_REQUEST['check_list'], true);
See in_array.

Posting label text along with the array checkbox values

I have following checkbox arrays in html form
<label><input name="columns[]" type="checkbox" value="pname" />Property Name</label>
<label><input name="columns[]" type="checkbox" value="2000" />Price</label>
<label><input name="columns[]" type="checkbox" value="New Road" />Location</label>
<label><input name="columns[]" type="checkbox" value="Joe Smith" />Owner</label>
there are about 30 such checkboxes.
All i want to do is sending the text along with its value of the checked box while form is posted. Lets say if i checked first checkbox, I want to send pname along with text Property Name. There can be several solutions for this, but don't know which one will be easier one.
The one I thought is. In submit page iterating through the loop of checkbox array, create another array and Push the text manually to this new array.
Or other option I have thought is, in value attribute I will send text with value like this pname^Property Name and in submit page i iterate through loop, explode the value and put them to respective arrays.
Is there any alternative solution for this?
Thanks
HTML
<label><input name="columns['Property Name']" type="checkbox" value="pname" />Property Name</label>
<label><input name="columns['Price']" type="checkbox" value="2000" />Price</label>
<label><input name="columns['Location']" type="checkbox" value="New Road" />Location</label>
<label><input name="columns['Owner']" type="checkbox" value="Joe Smith" />Owner</label>
PHP
$columns = $_GET["columns"]; // or $_POST
echo $columns["Property Name"] ; // pname
echo $columns["Price"] ; // 2000
echo $columns["Location"] ; // New Orad
echo $columns["Owner"] ; // Joe Smith
You can use like this
$posted_columns = implode(',',$_POST['columns']);
print_r($posted_columns);
but your code should be like this
<label><input name="columns['Property Name']" type="checkbox" value="pname" />Property Name</label>

Checkbox Array only showing checked values - PHP

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.

alter $_POST data of radio select option

So I have a Radio button group, like so:
<div data-role="fieldcontain">
<fieldset data-role="controlgroup">
<legend>Choose a pet:</legend>
<input type="radio" name="radio-choice-1" id="radio-choice-1" value="choice-1" checked="checked" />
<label for="radio-choice-1">Cat</label>
<input type="radio" name="radio-choice-1" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">Dog</label>
<input type="radio" name="radio-choice-1" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="radio-choice-1" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">Lizard</label>
</fieldset>
</div>
Now if I just submit the for I get this $_POST (Note I have multiple Radio group questions)
Array
(
[radio-choice-1] => choice-1
[radio-choice-2] => choice-4
[radio-choice-3] => choice-2
[submit] => submit
[PHPSESSID] => 11111111111111111
)
How Can I restructure the HTML or $_POST data before submission to make it look like this:
Array
(
[type-1] => radio-choice-1
[answer-1] => choice-1
[type-2] => radio-choice-2
[answer-2] => choice-4
[type-3] => radio-choice-3
[answer-3] => choice-2
[submit] => submit
[PHPSESSID] => 11111111111111111
)
Maybe jQuery as an option?
You can do it post submission, the code below should work if you keep your original naming conventions.
$postValues = $_POST;
$altered = Array();
$unaltered = Array();
foreach ($postValues as $key => $val) {
if ( FALSE !== stripos($key, 'radio-choice-') ) {
$num = explode('-', $key);
$num = $num[2];
$altered['type-'.$num] = $key;
$altered['answer-'.$num] = $value;
} else {
$unAltered[$key] = $value;
}
}
$manipulatedPOSTData = array_merge($altered, $unAltered);
// Keep doing what you intended
I assume you are talking about a form that you are submitting?
If you want to add finer control over what gets posted, you can do one of two things:
1) add hidden variables
or
2) use jQuery .post() (http://api.jquery.com/jQuery.post/) instead of doing a normal form submission.
Personally, I think the first of the two is simplest:
<div data-role="fieldcontain">
<input type="hidden" name="type-1" value="radio-choice-1" />
<fieldset data-role="controlgroup">
<legend>Choose a pet:</legend>
<input type="radio" name="answer-1" id="radio-choice-1" value="choice-1" checked="checked" />
<label for="radio-choice-1">Cat</label>
<input type="radio" name="answer-1" id="radio-choice-2" value="choice-2" />
<label for="radio-choice-2">Dog</label>
<input type="radio" name="answer-1" id="radio-choice-3" value="choice-3" />
<label for="radio-choice-3">Hamster</label>
<input type="radio" name="answer-1" id="radio-choice-4" value="choice-4" />
<label for="radio-choice-4">Lizard</label>
</fieldset>
</div>
By changing the name of the radio ground to answer-1 and adding a hidden variable, that should meet your requirements (do the same for your other radio elements).
Avoid client side elaboration if you can.
Otherwise use .submit() and hand code what you need. but it's harder.

Categories