Is there a way of assigning the value of a checkbox as an array.
I have a load of checkboxes, one of which is an all option. I would like to set the value of this to an array?
I have tried creating an array of ints (ids) from an array of my objects using a simple loop and then using print_r in the value (I know this is a bit ugly and I can see why this wouldn't work but can't seem to find the correct syntax).
$arrAllID = array();
foreach ($AvailableGroups as $objASRDCallBackGroup)
{
$arrAllID[] = $objASRDCallBackGroup->m_iGroupID;
}
<input id="group-name" value="<?php print_r($arrAllID) ?>" name="SelectedGroups[]" type="checkbox">All
Also I could design around this and do the retrieval of the collection when the form is posted by checking if all was selected or something but I would really like to know how you can do this now..
Any help greatly received.
Thanks
You can use the HTML5 data attribute to store your array of values. I'm not very familiar with PHP, but the data attribute will render on the client, and if this checkbox is clicked, you can loop through the array in the data attribute and do whatever you need to send data back to the server.
<input id="group-name" data-allid="<?php print_r($arrAllID) ?>" name="SelectedGroups[]" type="checkbox">All
function someClickHandler() {
var chkGroupName = document.getElementById('group-name');
var allIds = chkGroupName.attr('data-allid');
// do what you need with the array of ids.
}
Related
With php, I dynamically create a form with a variable number of "select" in it.
When user has done his selections, he hits a button that launches another php.
I now want to browse through all form elements, but have no idea on how to get the array of form elements with their respective name.
I know how to retrieve the value for a given select when I have its name, but as the select elements in the form are different each time, I cant use the name directly, but would need the array of form elements.
So, as I understand the question: form is created dynamically (with dynamic names) so you don't know what's being POST'd and what the other options were in your handler. I suggest using javascript to solve this - in the form, add an onSubmit="saveForm();" and then, in that function saveForm, you'll have to grab all the form elements and put them into some format your PHP can interpret. Pseudocode:
function saveForm(){
var outputString="[";
var theInputs=document.querySelectorAll("input, select");
for (var x=0; x<theInputs.length; x++){
outputString+="\""+theInputs.name+"\", ";
}
outputString=outputString.substring(0,outputString.length-2)+"]";
var hiddenInput=document.createElement("input");
hiddenInput.name="formNames";
hiddenInput.value=outputString;
document.body.appendChild(hiddenInput);
}
That way, a variable will be set in $_POST['formNames'] that will be equivalent to a string representation of an array containing all the names of the form elements. You can loop through that array to get the $_POST data from each one of them.
I found the solution. I can do:
foreach($_POST as $eid){
and $eid will then be every element I have in my form
I am having a form with quite a lot of list boxes. After submitting the form I have no problem to process all list boxes with PHP in a loop. But I am looking for a way to only grab those that have changed because it would save a lot of processing time.
Let's say I have a hundred list boxes. Their ids are "lb_1" ... "lb_100". I would loop through them like:
foreach($_POST as $key=>$value) {
if (substr($key,0,3)== "lb_" ) {
...do something...
}
}
That loop however will do something with all the hundred listbox values. I only want to catch those that have actually changed.
Any ideas?
To expand on the suggestion given by #Tushar, you could use Javascript to set the Disabled attribute to true for any field that has not changed. That way they would not exist in the POST. The only way you would know which ones to disable is to store the initial values (in JS probably for ease of comparison). Then on form submit, loop through the fields and disable all the ones that have not changed.
document.getElementById('lb_1').disabled = true; // example of how to disable field
I can provide more example code if you like.
Ok, thanks to your tips I came up with this client-side solution:
I added a hidden form text field right before each listbox that contains the original value. The hidden field and the list boxed are named with the same unique suffix so I know which one belongs to which.
When the form is passed the PHP loop looks like this:
foreach($_POST as $key=>$value) {
if (substr($key,0,3)== "lb_" ) {
if ($_POST[hidden_name] != $value) {
setNewValue($value);
}
}
}
This is a lot faster than accessing the database for each compare since the values are already available in the $_POST array.
Thanks for your help.
I am currently working on a javascript and php project. I have a checkbox array that have the same id chkCategory and the name chkCategory[]. I then have PHP return some json that contains the various categories and I want to set each checkbox checked attribute for each category that was in the json. The only thing that identifies each checkbox is the value. I.e. the json may have a value MyCategory so the checkbox that has the value MyCategory should be set.
I know you can use $("#myCheckBoxId").attr("checked", true) for an individual checkbox but not sure how to do this for a specific checkbox with a certain value.
Thanks for any help you can provide.
You can use standard CSS attribute selectors in JQuery. For example:
$("input[value=MyCategory]")
will select the input tag with attribute value=MyCategory. If there are different types of inputs with that value, you could also select on type=checkbox:
$("input[type=checkbox][value=MyCategory]")
If you know the index, you could do...
$("input[name='MyCategory']").eq(n).prop("checked", true);
If you don't have jQuery...
document.getElementsByName("MyCategory")[n].checked = true;
In plain javascript it would look like this:
document.querySelector('input[value=checkboxValue]').checked = true;
where checkboxValue is the value of your checkbox
i want to set more value to checkbox,how do it? this is my code: from this code only i could get upto 3 value,why? i want to get 5 value.how do it?
$results=$watch.",".$thumbnail.",".$name.",".$description.",".$val;
<input type="checkbox" name="checkbox[]" id="checkbox[]" class="addbtn" value=<?php echo $results;?>
javascript fn:
function chkbox()
{
$('[name^=checkbox]:checked').each(function() {
var ckballvalue=($(this).val());
var latlngStrs = ckballvalue.split(",",5);
var link = latlngStrs[0];
var thumbnail = latlngStrs[1];
var name = latlngStrs[2];
var description = latlngStrs[3];
var categ = latlngStrs[4];
alert(link+thumbnail+name+description+categ);
}
Assuming your markup is correct and you're using POST, then you just grab them from the $_POST variable in PHP.
$checkedValues = $_POST['checkbox'];
This will be an array of all checked values, where 'checkbox' is the name of your input group.
When a browser posts a form containing checkboxes, it only posts anything for the checkboxes that have been checked. The ones that are unchecked will not be sent at all.
Therefore, if you have five checkboxes, but only three are checked, your PHP $_POST or $_GET array will only contain entries for those three, not the other two.
This will also means that if you're using name="checkbox[]" for all your checkboxes, then your array elements will not be numbered as you expect.
Note that this is the way HTML forms work; it's got nothing to do with PHP.
If you want to force it, you could have a hidden field on the form which mirrors the checkbox state using Javascript. This way you'll be certain you'll always get the value posted to you whether it was checked or not.
Or you could just accept the way it works. You may not get the values posted to you if they aren't checked, but their absence is sufficient, as not having those field posted allows you to know that they weren't checked.
You might find it helpful to give your checkboxes explicit array element keys -- ie rather than name="checkbox[]", use name="checkbox[3]". This way they'll always be posted into the right slot in your $_POST array.
Hope that helps.
I'm posting a form to a php script. The form contains a dynamic number of fields named cardObjectX, where X is a counter. Example: cardObject1, cardObject2, and so on. I need to loop through all the cardObject fields in my php script, but because we don't know how many there will be for any given post, we can't hard-code the field names.
Is there a way I can grab an array of all the fields that start with cardObject?
this should help you get started:
foreach($_POST as $key=>$value) {
if(strpos($key,"cardObject")!==FALSE) {
//do something with this cardObject...
}
}
<input name="cardObject[1]" value="">
using this naming style in your inputs makes it possible to access these inputs as an array in php like this:
$_POST['cardObject'][1]
or loop throug every cardObject like this:
foreach($_POST['cardObject'] as $cardObject){
}