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.
Related
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 am displaying products from my Product database in a table, with a checkbox to select the product that is desired.
Each product has 3 variants or price point options, entered in the database as seperate items for each price point variation.
Code category Description Points Choose your Points
1001-B Logo artwork supplied Basic 4.00 points
1001-S Logo artwork supplied Standard 6.00 points
1001-P Logo artwork supplied Premium 12.00 points
1002-B Logo re-draw to vector Basic 6.00 points
1002-S Logo re-draw to vector Standard 8.00 points
1002-P Logo re-draw to vector Premium 14.00 points
There are 3 price point variations for every product: Basic, Standard and Premium. Each "Product" has the same Product Code. So as above Product: 1001-B is the Basic, 1001-S is the standard and 1001-P is the premium.
So the customer chooses which Price point option they want by selecting the checkbox next to each item.
I am wanting to only allow them to select one price point / product for each product pairing (ie only one of the 3 price points).
So basically to get the checkbox to work like a radio button. I cannot use a radio button because I am using the same name for all of the items in my checkbox array: name="id[]"
echo '<input type="checkbox" name="id[]" value="' . $id . '" /></td>';
How can I achieve this?
I know I can do the following:
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<input type="checkbox" class="radio" value="1" name="fooby[1][]" />
<p> </p>
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
<input type="checkbox" class="radio" value="1" name="fooby[2][]" />
$("input:checkbox").click(function() {
if ($(this).attr("checked") === true) {
var group = "input:checkbox[name='" + $(this).attr("name") + "']";
$(group).attr("checked", false);
$(this).attr("checked", true);
} else {
$(this).attr("checked", false);
}
});
But this relies on the fact that name="fooby[1][] name="fooby[2][] etc changes. I cannot do that in my case.
Any ideas?
I have already got the cart aspect of this after the page is submitted resolved so I don't want to make fundamental changes to the checkbox format I have.
WHenever you have repeating modules within a page it's easy to isolate instances by traversing up to a main parent of the instance and searching within that parent only.
HTML
<div class="product">
<div class="name"/>
<div class="description/>
<div class="choices">
<input type="checkbox"/>
</div>
</div>
JS:
$('.product input:checkbox').change(function(){
if(this.checked){
$(this).closest('.choices').find('input:checkbox').not(this).prop('checked',false);
}
});
No knowledge of any properties of the checkboxes is required using this generic traverse pattern. If checkbozes aren't wrapped in labels can shorten to:
$(this).siblings().prop('checked',false);
Let's add class attribute to each group and handle with it:
<input type="checkbox" value="1B" name="id[]" class="group1" />
<input type="checkbox" value="1S" name="id[]" class="group1" />
<input type="checkbox" value="1P" name="id[]" class="group1" />
<p> </p>
<input type="checkbox" value="2B" name="id[]" class="group2" />
<input type="checkbox" value="2S" name="id[]" class="group2" />
<input type="checkbox" value="2P" name="id[]" class="group2" />
$("input:checkbox").click(function() {
$('input:checkbox[class="' + $(this).attr('class') + '"]').prop('checked', false);
$(this).prop('checked', true);
});
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']"/>