psychowolvesbane Posted February 23, 2008 Share Posted February 23, 2008 I got 7 checkboxes which are basically sizes for clothing, XS, S, M, L, XL, XXL, and 3XL and I am trying to validate them using javascript, all I need is for it to know if any have been checked, and if none haven't bring up the warning message I already have in place. The only problem I've been getting so far is accessing the check button group I've made, which is an array called Sizes[]. Here's the form code: <?php $CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL"); ?> <span id="mySpan7" class="errmsg"><?php echo $Message_ASizes ?></span><br> <span class="head4">Available Sizes:</span><br> <table id="Sizes"><tr> <?php $X=0; while($X!=7) { $Size = $CBSizes[$X]; echo"<td>$Size:</td>"; echo"<td><input type='checkbox' name='Sizes[]' value='$Size'"; if($SizeA[$Size]==true){echo"Checked";}?>></td></tr><tr> <?php $X++; } ?> </tr></table> As you can see I use the array CBSizes to grab the names per value of $X, and place all the Sizes into the Sizes[] array. I have a method already in PHP for validating it but I haven't got a working one for Javascript. Here's the code that's in my main validation function: numBoxesSelected = 0; var valuesSelected = new Array; for (var i=1; i <= numBoxes; i++) { if (eval("document.AddProduct.Sizes" + i + ".checked")) { valuesSelected[numBoxesSelected] = eval("document.AddProduct.Sizes" + i + ".value"); numBoxesSelected++; } } if(numBoxesSelected ==0) { document.getElementById('mySpan7').innerHTML='! Please choose any relevant Sizes!'; } else { document.getElementById('mySpan7').innerHTML=''; } Thanks in advance for those that help. Quote Link to comment https://forums.phpfreaks.com/topic/92650-check-button-validation-help/ Share on other sites More sharing options...
phpQ8-ton-R Posted February 23, 2008 Share Posted February 23, 2008 I personally would do this; I would give each input box a separate id and access them individually when you validate with JS. Set a variable in your PHP script; something like: <?php $CBSizes = array("XS","Small", "Medium", "Large", "XL", "XXL", "3XL"); ?> <span id="mySpan7" class="errmsg"><?php echo $Message_ASizes ?></span><br> <span class="head4">Available Sizes:</span><br> <table id="Sizes"><tr> <?php $X=0; $i="0"; while($X!=7) { $i = $i + 1; $Size = $CBSizes[$X]; echo"<td>$Size:</td>"; echo"<td><input type='checkbox' id='field$i' name='Sizes[]' value='$Size'"; if($SizeA[$Size]==true){echo"Checked";}?>></td></tr><tr> <?php $X++; } ?> </tr></table> Then validate your checkboxes that way. Quote Link to comment https://forums.phpfreaks.com/topic/92650-check-button-validation-help/#findComment-474815 Share on other sites More sharing options...
psychowolvesbane Posted February 24, 2008 Author Share Posted February 24, 2008 I get what you're saying but I'm unsure how to change what I already have in my function to access the id's, because the only way I know how to access an element by it's id is document.getElementById. So in this case would it be document.getElementById(' "Sizes"+i ')? Quote Link to comment https://forums.phpfreaks.com/topic/92650-check-button-validation-help/#findComment-474825 Share on other sites More sharing options...
psychowolvesbane Posted February 24, 2008 Author Share Posted February 24, 2008 BTW I have got this solved via another source, the code I used without using ID's was: var valuesSelected=[]; var checks=document['AddProduct']['Sizes[]'], i=0, t='', c; while(c=checks[i++]) { c.checked?valuesSelected[valuesSelected.length]=c.value:null; } !valuesSelected.length?t='! Please choose any relevant Sizes!':null; document.getElementById('mySpan7').innerHTML=t; Topic Solved! Quote Link to comment https://forums.phpfreaks.com/topic/92650-check-button-validation-help/#findComment-475180 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.