How to set checkbox to a default value if not checked

For checkbox:

<input
    type="checkbox" 
    id="i88ca_id"
    name="i88ca_id"
    value="i88ca_value"
    checked="checked"
/>

 you can test for the checkbox by checking whether getParameter() returns null or not:

HTTPServetRequest request = ...;
boolean i88ca_state = request.getParameter( "i88ca_id" ) != null;

If the checkbox is checked by the user in the above example getParameter will return "i88ca_value".

If you want to have a default value when the checkbox is not checked, for example, in PHP, you can do this:

$checkbox = isset($_POST['i88ca_id']) ? $_POST['i88ca_id'] : 0 ;

If it is inconvenient for you to deal with it in the backend and you have to deal with it in the web page, you can use Javascript from the front-end:

Put an hidden value:


<input
    type="hidden" 
    id="i88ca_id_hidden"
    name="i88ca_id"
    value="default_i88ca_value"
/>


if(document.getElementById("i88ca_id").checked){
  document.getElementById('i88ca_id_hidden').disabled = true;
}else {
document.getElementById('i88ca_id_hidden').disabled = false;
}

Note: you need the "else" clause if you want the page to work repeatedly. Or else it can only work for the first time and the element of 'i88ca_id_hidden' is always disabled from then on.

Comments

Popular posts from this blog

How to get Active Transactions and Locks from MySQL

Example of a PAC file