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
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
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.
}
I got the following issue. A clients wants that the text after checkboxes are links to other pages and thus between ...
I have the following code:
$form['boxes_brands'] = array(
'#type'=>'checkboxes',
'#title'=>'<div id="title-container">Merken</div>',
'#options'=>$brandArr,
'#default_value'=>$_SESSION['filter_brands_cat'],
);
=> $brandArr is an array of brands.
I looked in the Form Api of Drupal but I did not find an option to do this. I could alter the values in $brandArr but of course that changes the value of the value attribuut of the input object too.
Using the prefix and suffix options won't do it either because I don't want the checkboxes in the tags too.
Is there a clean way to do this?
Thanks!
If you created the form with the UI, then you should be able to specify something like this in as the options and links would be rendered as links:
google|This is a link to google
yahoo|Yahoo
bing|Bing!
See example:
Otherwise, you should be able to modify the $brandArr accordingly to create links in the label. Doing this should NOT change the value of the attribute as it should be a $value->$label associative array. You just need to change the $label not the $value.
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 have got a form which a user can use to create a new store and to edit an existing one. When this form is being used to edit a store there are certain fields that I want the user to see but not edit eg. store_id. I have explored the different Zend_Form_Elements hoping to find some kind of static element but with no luck.
So my question is, how do I display information using Zend_Form that a user can't edit?
Thanks.
readonly alone is not enough, because users will still be able to edit it if they really want. You should use $element->setIgnore(true) that will ensure that Zend_Form_Element won't try to populate the element from POST/GET, and I'd double check that also. You have to make sure the values you are getting into the databases can never contain this element.
Finally, if you would like your element to be displayed in a different way than just with readonly, you can do that by changing the element decorators.
I just managed to work this one out myself. The solution was to change the view helper on the elements to the formNote helper eg. $element->helper = 'formNote'. The result of this was that the value gets displayed as straight text instead of being inside a form element.
Thanks for your answers.
That's very good solution when you don't need to populate the element value when the form is submitted.
It's equivalent solution is to use the Form Element method setAttrib() and disable the form element
$formElement->setAttrib('disable','disable')
which will only freeze the element.
But if you need to populate the field, using the previous solutions you will probably need additional hidden field added, which will pass the value. Developing custom form element will be good style but that's not welcomed by each developer so you can use some tricky way to set a form element as a text only but populate its value. That way is when you create the element as a hidden field, set its value and use the Form Element method setDescription() to set and display the element text value.
$formElement = new Zend_Form_Element_Hidden( 'elName',
array( 'label' => 'elLabel', 'value' => 'elValue' ) );
$formElement->setDescription( 'elValue' );
Then you can render that hidden element and display the value with the
$formElement->getDescription().
$element->setAttrib('readonly', 'true');
http://www.w3.org/TR/html401/interact/forms.html#adef-readonly
According to Amr Mostafa, if you use:
$element->setAttrib('readonly', 'true');
OR
$element->setAttribs(array('disabled' => 'disabled'));
User still send values by POST/GET and they are stored in DB.
The only way for me to don't taking into account the values from POST/GES is:
$element->setIgnore(true)
Example:
$element = new Zend_Form_Element_Text('element');
$element->setIgnore(true);