by prettyscripts on 2008-07-03 12:16:33
misc • javascript • html • xajax
in one of the forms i work on, i need to set other form fields based on the value of a checkbox. the value this.checked was passwd a parameter to xajax function.
Code:
<input type="checkbox" name="checkbox1" id="checkbox1" value="Y" onClick="xajax_function(this.checked);" /> |
n xajax function, the value of the parameter is either true or empty value.
PHP:
function xajax_function($data) { | |
$obj = new xajaxResponse(); | |
$obj->assign("field1", "value", $data ? "true value" : "false value"); | |
return $obj; | |
} |
the problem is, the value in field1 always display “true value” whether the box is checked or not!
it turns out that the true passed is a string! not boolean! you need to compare $data and “true” (with quotes).
replace line 3 of above php code:
PHP:
$obj->assign("field1", "value", ($data == "true") ? "true value" : "false value"); |
now it works as intended.
alternatively check with isset($data) to see if checkbox is checked.
to check the opposite value, ie checkbox is not checked, use !isset($data).
note: do not use / pass this.value as parameter. the value will always be the value assigned to checkbox, in this case, always “Y".