Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

HTML submit multiple values through one check box?

Tags:

html

forms

Hello I have a form that allows the user to check as many options as they like and then hit submit. Is there any way to have the input type 'checkbox' submit more than one value?

For example right now I have:

<input type="checkbox" value="testuser">

But I want something like:

<input type="checkbox" value="testuser" valuetwo="1">

Is there any way to achieve this second option? Thanks!

Since there is no way to submit to values, is there a way to submit them both in value one? For example:

<input type="checkbox" value="testuser,1">

And if so how would I separate the value into two?

like image 962
T Neate Avatar asked Mar 25 '15 19:03

T Neate


People also ask

Can a checkbox have two values?

Checkbox is used to make a choice between two options. Input from each checkbox is made to a single variable. A checkbox can pass one of the two values to this variable - one value for the checked state and another one for the unchecked state.

Does checkbox allow multiple selection?

Checkboxes are objects of a HTML form which behaves like a toggle switch. i.e, a checkbox can be in one of the two states, either checked or unchecked. From a group of checkboxs user can select multiple options.

How do you make a checkable box in HTML?

The simplest way to create a checkbox in HTML is by using the input tag. We have set the input type to “checkbox” as you can see in the example code. The name attribute lets us give a name to the checkbox, and with the value attribute, we specify the value it holds.


1 Answers

From your comments, it sounds like you have some JavaScript that handles the data before it's submitted. If that's the case, you can add a data attribute to the checkbox. To use your example, you could call it data-valuetwo.

<input type="checkbox" value="testuser" data-valuetwo="1">

Then, your JavaScript can use getAttribute to retrieve the value in your data-valuetwo attribute and handle it appropriately. It could look something like this:

var valuetwo = checkbox.getAttribute("data-valuetwo");
like image 157
Josiah Keller Avatar answered Nov 20 '22 16:11

Josiah Keller