Forum Moderators: open

Message Too Old, No Replies

Enable checkbox based on Y or N from hidden field value

         

leebridgewater

4:42 pm on Jul 28, 2009 (gmt 0)

10+ Year Member


Hi,

I do not know much about Javascript, but I am hopeful that someone on here may have the answer for me!

I have a PHP document which has a recordset pulling data from a table. I have setup a form and I have placed a hidden form field within the form called "chargerIncluded". This hidden form form field is populated from the recordset as either Y or N.

I would like to enable or disable a form checkbox based on whether the charger was included (enabled if value of hidden form field is Y; disabled if value of hidden form field is N).

This is what I have got so far, but it does nothing!


function enableChkbox(check)

{if (document.form.chargerIncluded.value == 'Y')
{document.form["confirmChkbox"].disabled = false;
}
else
{document.form ["confirmChkbox"].disabled = true;
}
}

daveVk

2:01 am on Jul 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Why have 2 form elements hidden and checkbox ? Store state directly into checked property on checkbox, and save a lot of copying.

else try

document.form ["confirmChkbox"].checked = ...

Fotiman

1:28 pm on Jul 29, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Welcome to WebmasterWorld!
This is not a good case for using JavaScript, as it will only work for those users that have JavaScript enabled (instead, use JavaScript to progressively enhance your pages, not as a required component).

If you PHP looks something like this:


<input type="hidden" name="chargerIncluded" value="<?php
echo $chargerIncluded;
?>">

Then you could do:


<input type="hidden" name="chargerIncluded" value="<?php
echo $chargerIncluded;
?>">
<input type="checkbox" name="confirmChkbox" <?php
if ($chargerIncluded == 'N') {
echo 'disabled="disabled"';
}
?>>

I think that would work, and will work regardless of whether your end user has JavaScript enabled.