0

Looking to create one single check-box to pass two different types of values like opting in.

So something like if this is true:

<input type=checkbox checked=true name="opted_in">

if false:

<input type=checkbox name="opted_out">

Am I on the right track? If so how can I nest it?

4
  • The name doesn't change depending on whether it's checked. Commented Apr 6, 2015 at 20:40
  • What do you mean by nesting it? Commented Apr 6, 2015 at 20:40
  • If the box is unchecked, then pass the value of 'opted_out'. Commented Apr 6, 2015 at 20:42
  • That cannot be done. But you can assign the value based on the checkbox is checked or not in javascript (I guess your are trying to pass value to javascript): if(document.getElementByName('opted_in').checked) value = "opted-in"; else value = "opted-out"; Commented Apr 6, 2015 at 20:44

2 Answers 2

3

You are trying to impose a different functionality on something that clearly already has a defined way of working.

Use it the way it is meant to be used which is to use the checked attribute to see if it is checked or not.

Sign up to request clarification or add additional context in comments.

Comments

1

The way that checkboxes work is that the value is only submitted if the box is checked. If the box is not checked, nothing is submitted for that element in the form. In the server code, you need to check whether the parameter with the given name exists. E.g. in PHP you would write:

if (isset($_POST['opted_in']))

If you want something to be submitted in both cases, you could use radio buttons instead of a checkbox:

<input type="radio" name="option" value="opted_in" checked>
<input type="radio" name="option" value="opted_out">

Or you could use a menu:

<select name="option">
    <option value="opted_in">Opt In</option>
    <option value="opted_out">Opt Out</option>
</select>

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.