need to require that at least one of 9 checkboxes be checked - html

I have a form that has a save button in it. I also have 10 checkboxes that I need to require the user to check at least one of them, then another set of 6 checkboxes that I need the user to check at least one of them. Is there a way to have a message pop up when the user tries to save the record without having made a selection in both of these areas?

Yes, you need a validated form with jQuery.
First thing is that your HTML will need a checkbox array like:
<input type="checkbox" name='cb[]' value='cb1'/>
<input type="checkbox" name='cb[]' value='cb2'/>
<input type="checkbox" name='cb[]' value='cb3'/>
<input type="checkbox" name='cb[]' value='cb4'/>
<input type="checkbox" name='cb[]' value='cb5'/>
if(jQuery('input[name=cb]:checked').length==0) {
console.log('Please check at least one checkbox');
}
Something like that...For your other checkbox group, use a different name like name="cb2[]" and jQuery would be:
if(jQuery('input[name=cb2]:checked').length==0) {
console.log('Please check at least one checkbox');
}
I hope this helps.

Related

Multiple Forms with same content boxes on one page

I'm looking for a solution to generate multiple forms on one page, that contain the same checkboxes and fields. For now, when I create those forms, even if I use different form names, all checkboxes with the same name get checked at the same time.
What I am trying to do is the following. I do have an events-list. Every event should have a form attached. Every form has some text and some checkboxes. But when I'm trying to check some boxes in my second form, it jumps to the first one (because of the same name of the checkbox).
<form method="post" name="form1">
<input type="checkbox" name="checkbox1" />
<input type="checkbox" name="checkbox2" />
</form>
<form method="post" name="form2">
<input type="checkbox" name="checkbox1" />
<input type="checkbox" name="checkbox2" />
</form>
Is there a way to get this working? Or do I have to use unique checkbox names, even if the formnames are unique? Which would make it more complex when I have a variable count of forms / events.
You can dynamically bind form elements i.e checkboxes. Don't include it in HTML code. This can be done with Jquery $.html() function to bind checkboxes to forms at runtime.
I guess this happens because form1 overrides form2 elements due to same names and id of checkboxes.
Or Try to use 4 different id of all checkboxes, and different id's of form names.
Hope it may work.
Happy Coding!
Atul Jindal

Radio buttons in array for edit form

I have form that has rows which send data in array. Everything works ok, only problem is with radio buttons, when I want to edit data, they are printed with checked=checked attribute, but all browsers only register last ckecked radio button. I have tried everything I can think of, not even hack with jQuery works and jQuery does the same.
<input type="radio" name="targets[image][0]" value="/images/Targets/target2.png">
<input type="radio" name="targets[image][0]" value="/images/Targets/target1.png" checked="checked">
<input type="radio" name="targets[image][0]" value="/images/Targets/target3.png">
<input type="radio" name="targets[image][1]" value="/images/Targets/target2.png">
<input type="radio" name="targets[image][1]" value="/images/Targets/target1.png" checked="checked">
<input type="radio" name="targets[image][1]" value="/images/Targets/target3.png">
When I submit the form, then it sends only last radio as checked. Any idea how to solve this?
EDIT: For handling submit there is simple PHP script that handles $_POST
I dont want to selecte multiple values for one group, but it seems that it takes all those groups as one group, its like it ignores index in array [0]
The attribute is just "checked". You don't need "checked=checked".
Try this:
<input type="radio" name="targets[image][0]" value="/images/Targets/target1.png" checked>
No solution that I tried seemed to work, so I have reorganized my js file and now my jQuery hack works, so for anyone with similar problem, try:
$('[checked=checked]').each(function(){
$(this).click();
});
Personally I dont like this solution, feels unprofessional, but it works and I have to have radio buttons there.

How do I make radio buttons outside of forms?

I'm trying to program a dynamic form, so I can't use the normal form tags and stuff. I use normal buttons, JQuery, and AJAX calls to simulate a traditional form. However, I can't figure out how to do radio buttons. Any help?
EDIT: Yeah, I suppose I should have been more specific. I tried doing
<input type="radio" />
and stuff, but:
it lets me select more than one button at a time (which kind of defeats the point of radio buttons)
it won't let me deselect a button after it's pressed!
EDIT 2: The reason I'm not using form tags is that I need multiple submit buttons as well, and the only solution I found to that dilemma was to not use form tags.
Why can't you use the form tag? There is nothing stopping you from doing so. But if you don't want to use the form tag, why not:
<input type='radio' name='test' value='1' checked>
<input type='radio' name='test' value='2'>
<input type='radio' name='test' value='3'>
<input type='radio' name='test' value='4'>
Works fine for me. Demo
Edit:
1: You need to specify a name for the radio group, otherwise each input is considered its own group. Hence why you can select more than one button at a time when using <input type="radio" />. Look at my code above. The radio group is 'test'.
2: Radio buttons are suppose to have a default value. When you create a radio group you should be specifying a default value with the checked attribute. A consequence of this is that you can't deselect a radio button. You can either choose a different value or stick with the default. If you want to be able to deselect, then consider using checkboxes instead. I've updated the example code to reflect this.
If you are able to select more than one radio button, its sounds like your name attributes are not matching. What you want to end up with is something like the following:
<input type="radio" name="group-1" value="something-unique">
<input type="radio" name="group-1" value="something-else-unique">
<input type="radio" name="group-1" value="another-unique-something">
<input type="radio" name="group-2" value="something-unique">
<input type="radio" name="group-2" value="something-else-unique">
<input type="radio" name="group-2" value="another-unique-something">
Note that the name attribute is the same for the group of options, which means that the selections will replace each other.
Also, I haven't had any issues not wrapping radios in form tags, when using them purely with javascript, however if you want to do any html post stuff, I would expect that they are required.
You can try this:
HTML
<div>
<ul>
<li><input type="radio" name="radio" value="value1" checked>Radio Button1</input></li>
<li><input type="radio" name="radio" value="value1">Radio Button2</input></li>
<li><input type="radio" name="radio" value="value1">Radio Button3</input></li>
</ul>
</div>
DEMO
There should not be an input element without a form element. You are not going to get the HTML to respond the way you want it to if you do not use it correctly. Multiple submit buttons would indicate the need for multiple forms.
If, for whatever reason, that does not work, perhaps you should reconsider the format through which you are asking the user to submit information.

What is the purpose of the `name` attribute in a checkbox input element?

I went through many online documents for the checkbox input in XHTML. Can anyone clear my doubt? What does this name field actually stand for?
Milk: <input type="checkbox" name="checkbox" value="Milk">
Chocolate: <input type="checkbox" name="checkbox" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox" value="Cold Drink">
I thought it was an identifier for that particular checkbox, which can later be used in other file by just referring their name, but given that all the checkbox had same name, why even specify it? A little confused of this.
Dont be confused because of name="checkbox". It could more logically be name="drink" and type=checkbox.
In the above case, you have multiple checkboxes with the same name. When several checkboxes have the same name, the form will send a group of values to the server in the request. Note: only the values of the checked checkboxes will be sent to the server.
Ideally these are used to allow multiple choice questions where more than one answer is allowed.
As opposed to radio buttons, where only one answer is allowed among the options.
Update:
On the receiving side, if you're using JSP for example - the values of the selected checkboxes will be available as request.getParameterValues("drink") or request.getParameterValues("checkbox") in your actual case. This is where the name attribute is used.
The name attribute is used to
reference form data after it’s
submitted, and to reference the data
using JavaScript on the client side.
Source: http://reference.sitepoint.com/html/input/name
Basically, what you've described. When the form is submitted, you can access the values of the form elements through the name you ascribe to them.
The only place where you would want to have multiple inputs with the same name is when they are radio buttons, in which case it is used to indicate which one of them belongs to the same group and thus only one of which can be selected at a time.
The name attribute is used to identify a checkbox. Which you could parse into a object like so {checkboxName1: 'checkboxValue2', checkboxName2: 'checkboxValue2'}
You missed the array setting for the name. By using the array setting (using square brackets), the result will be three different indexes for the checkboxes.
Milk: <input type="checkbox" name="checkbox[]" value="Milk">
Chocolate: <input type="checkbox" name="checkbox[]" value="chocolate">
Cold Drink: <input type="checkbox" name="checkbox[]" value="Cold Drink">
the "name" is same with the databse record, every field should have a name, so when you click the submit, the data will recorded to the database~~~~~

How do I get the result of a checked checkbox?

I'm really new to HTML, but I can't find anywhere how to return the variable from a check box. I've figured out how to get variables from forms and inputs, but I don't know how to get them from check boxes. Basically, I want a check box that if checked, will return the value yes. If not checked, it doesn't have to return a value, but no would be nice.
When submitting a form, if a checkbox is checked, it will be included in the values collection submitted to the server. If not checked, it will be omitted (as if that checkbox did not exist).
In JavaScript, you can find out whether the checkbox is checked by the checked property:
//returns true or false
var isChecked = document.getElementById('id_of_checkbox').checked;
In ASP.NET, the CheckBox control has a boolean property Checked.
Assign something to the value attribute. You should get the value of the checked box, if there is one in the form values on postback. Note that I've made it a radio input in the case where you want yes or no. For a radio you need to assign the same name, but different ids (if you assign an id at all). You'd want to use checkboxes in the case where you can have more than one value selected or only care to get the value when selected. These types of checkboxes should have different names.
<input type="radio" id="radio_yes" name="radio_btn" value="Yes" /> Yes
<input type="radio" id="radio_no" name="radio_btn" value="No"
checked="checked" /> No
If you only want a single check box with the value yes to be returned if checked, then you can use a check box. You won't get any value if it isn't checked.
<input type="checkbox" id="chk_box" name="chk_box"
value="Yes" /> Do you want this?
On the server-side, you look for the value corresponding to the "radio_btn" (first example) or "chk_box" (second example) key in your form parameters.
Use the 'checked' property.
var checkbox = document.getElementById('checkboxId');
var checked = checkbox.checked;
look at the value of YourCheckboxName.Checked
If a checkbox is disabled, it will be omitted in the values collection submitted to the server even if it is checked:
<input type="checkbox" name="checkbox1">
<input type="checkbox" name="checkbox2" checked>
<input type="checkbox" name="checkbox3" disabled>
<input type="checkbox" name="checkbox4" disabled checked>
Looks like:
result of HTML sample code
Only checkbox2 is in the values collection submitted to the server, if the user submits the form without any changes.