xajax and checkbox

by prettyscripts on 2008-07-03 12:16:33

miscjavascripthtmlxajax

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".

Tags: checkbox, form, xajax

1 comment

Comment by westbeck @ 2010-02-01 20:18:26
****-
I am using xajax.getFormValues(form) to send form values to the server side.

Before PHP 5.3.1 I could use
if [$aFormValues['CheckBox'] == "true") to test for set/unset

In PHP 5.3.1 returns error when checkbox is unset

I now have tested with isset() with

if (isset($aFormValues['CheckBox']) {
$objResponse->alert("Checkbox set");
} else {
$objResponse->alert("Checkbox unset");
}

This also results in error!

As my site is down since my site was upgrade to new PHP version I really need a way to get around this new behavior for checkboxes?

Leave a comment


Your email address will not be revealed on this site.
PoorExcellent
note: all comments are moderated. do not spam and do not advertise. only comments relevant to the post will be published.
(Line breaks become <br />)
(For my next comment on this site)
(Allow users to contact me through a message form -- Your email will not be revealed!)