[FX.php List] Problem with Javascript validating Checkboxes
Andrew Denman
adenman at tmea.org
Fri Jul 28 08:55:59 MDT 2006
The code won't work because to the javascript, 'en_trial_yes[]' is the name
of the checkboxes. Only PHP recognizes the [] in the name and creates an
array from the form. The problem is you cannot use [] in javascript without
escaping it, so this is the only way I've found to get to the checkboxes:
1. Place the form in a variable ('frm' below)
2. Reference the checkboxes with this: frm['en_trial_yes[]']
3. You can then use this name just like you would any other variable
<!-- Code -->
var frm = document.newConsult;
var IsEnteralReason = "";
for (var i = 0; i < frm['en_trial_yes[]'].length; i++) {
if (frm['en_trial_yes[]'][i].checked) {
IsEnteralReason = frm['en_trial_yes[]'][i].value;
}
}
if (IsEnteralReason == "") {
alert ("Please indicate the reason(s) the enteral trial was
unsuccessful!");
return false;
}
<!-- End Code -->
I had a revelation while typing this. I haven't tested it yet, but this
might/should work:
1. Put the checkboxes in a variable (ie: var en_trial =
document.newConsult['en_trial_yes[]'];)
2. And then use en_trial as the checkbox name in javascript (ie;
IsEnteralReason = en_trial[i].value;).
If you try this, let us know how it turns out.
Andrew Denman
_____
From: fx.php_list-bounces at mail.iviking.org
[mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Kim Hawksworth
Sent: Friday, July 28, 2006 7:55 AM
To: fx.php_list at mail.iviking.org
Subject: [FX.php List] Problem with Javascript validating Checkboxes
I'm trying to use Javascript to validate that at least one checkbox from a
checkbox field has been selected. The checkboxes are being pulled from a
value list in FM8. The php code that displays the checkboxes on the form
looks like this:
<?php
foreach ($getList ['valueLists']['en_trial_yes_list'] as $key => $value) {
?>
<input type="checkbox" name="en_trial_yes[]" value="<?php echo $value; ?>">
<?php echo $value; ?><br />
<?php
}
The Javascript code I'm using to validate the checkboxes looks like this:
var IsEnteralReason = ""
for (var i = 0; i < document.newConsult.en_trial_yes.length; i++) {
if (document.newConsult.en_trial_yes[i].checked) {
IsEnteralReason = document.newConsult.en_trial_yes[i].value
}
}
if (IsEnteralReason == "") {
alert ("Please indicate the reason(s) the enteral trial was
unsuccessful!");
return false;
}
For the life of me I can't get the Javascript to validate this field. Any
ideas what I'm doing wrong?
Thanks for your help.
--
Kim D. Hawksworth, R.Ph., Web Manager
The Ohio State University Health System
Department of Pharmacy
(614) 293-3765
kim.hawksworth at osumc.edu
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://www.iviking.org/pipermail/fx.php_list/attachments/20060728/a8f71184/attachment-0001.html
More information about the FX.php_List
mailing list