nychick
nychick

Reputation: 11

HTML checkbox to pass predefined value

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?

Upvotes: 0

Views: 491

Answers (2)

Barmar
Barmar

Reputation: 780899

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>

Upvotes: 1

SurrealSyntax
SurrealSyntax

Reputation: 1375

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.

Upvotes: 3

Related Questions