Vikhyath Maiya
Vikhyath Maiya

Reputation: 3202

single checkbox with yes or no values

Is it possible to have a single checkbox with two values ? i have a checkbox called "full-day ?".

 <div class="input-field col s2">
              <p>
      		  <input type="checkbox" id="test6" value="yes" ng-model="isFull"/>
     		   <label for="test6">Half Day</label>
    			</p>
            </div>
            

if the user checks it a yes should be passed when i press submit button and if the checkbox is not checked a no have to be passed.As far as i know if the checkbox is not activated html treats as if checkbox is not present and it wont be included in the query string.Here using radio button is not an option.Please let me know how do i go about it.

Upvotes: 0

Views: 17737

Answers (2)

Dave Carmean
Dave Carmean

Reputation: 89

This is simple:

<input type="hidden" id="test6" value="no" ng-model="isFull" checked>
<input type="checkbox" id="test6" value="yes" ng-model="isFull">

<label for="test6">Half Day</label>

  1. It is boolean.
  2. Code is processed sequentially
  3. Only the last 'checked' answer is used.

Therefore put the 'non' answer first but hide it and leave it always checked. The positive answer comes second.

If the form is editable (user can come back and change their answer) code to check the single visible checkbox appropriately e.g.

<?php if ($fieldValue=='Yes' ){ echo 'checked'; } ?>

Works for me!

Upvotes: 4

Quentin
Quentin

Reputation: 944075

Not really.

You can fake things with JavaScript that injects hidden inputs, but this is better handled with server side code.

e.g.

my $value;
if ($request->param("checkbox_name") {
    $value = "yes";
} else {
    $value = "no";
}

Upvotes: 0

Related Questions