doby48 Posted August 4, 2009 Share Posted August 4, 2009 My site is written in php but using Javascript on a page to verify that certain form fields are correctly completed before submitting the form. I am having no problem verifying text fields including my captcha but cannot seem to get it to seem to work for verifying if any checkboxes are checked. I only require that one box be checked. I have inserted the snip of code for verifying checkboxes just before my captcha verification and without the checkbox verification, captcha runs fine, but after I insert any code for checkboxes then the form verification stops at that point so I am obviuosly doing something wrong. Note that I am passing the data through without issues, the problem is only with the javascript verification. echo "if(document.form1.item.checked == false) {\n"; echo "alert('"; echo "Nothing checked"; echo "');\n"; echo "return (false);}\n"; I have also tried using the following echo "if (item.checked == 0) {\n"; echo "alert('"; echo "Nothing checked"; echo "');\n"; echo "return (false);}\n"; Here is a snip of code that is working correctly for a textbox from the same form: echo "if (form1.city.value.length < 3) {\n"; echo "alert('"; echo "Enter your city"; echo "');\n"; echo "form1.city.focus();"; echo "return (false);}\n"; What am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/ Share on other sites More sharing options...
dpacmittal Posted August 4, 2009 Share Posted August 4, 2009 Can you also post your form here? Try this in the meanwhile: echo "if(document.form1.item[0].checked == false) {\n"; echo "alert('"; echo "Nothing checked"; echo "');\n"; echo "return (false);}\n"; Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890331 Share on other sites More sharing options...
Psycho Posted August 4, 2009 Share Posted August 4, 2009 First off, when posting to the JavaScript forum, post the "rendered" javascript code, do not post the PHP that creates teh code as it makes it difficult to debug. The exception is is there is a problem with the PHP process of generating the JavaScript. Without seeing the checkbox fields themselves (as dpacmittal stated) it's kind of hard to provide a solution. But, I woud suggest that you make the checkbox fields an array to make this easier. Exmaple checkboxes Option 1: <input type="checkbox" name="options[]" value="1" /> Option 2: <input type="checkbox" name="options[]" value="2" /> Option 3: <input type="checkbox" name="options[]" value="3" /> Then, in your javascript use something like this: var noneChecked = true; var checkboxLen = form1['options[]'].length; for (var i=0; i<checkboxLen; i++) { if (form1['options[]'][i].checked) { oneChecked = false; break; } } if (noneChecked) { alert('Nothing checked.'); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890655 Share on other sites More sharing options...
doby48 Posted August 4, 2009 Author Share Posted August 4, 2009 I apologize for posting the Js code as I originally did. My form looks to be correct as I am getting the values passed through no problem and am using an array. But anyways the form is like the following: <input name="item[]" type="checkbox" value="service1" /> <input name="item[]" type="checkbox" value="service2" /> <input name="item[]" type="checkbox" value="service3" /> <input name="item[]" type="checkbox" value="service4" /> Let me try out the js that was posted by mjdamato and see if that works. Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890687 Share on other sites More sharing options...
doby48 Posted August 4, 2009 Author Share Posted August 4, 2009 I'm getting prompted now but unfortunatly its now telling me nothing checked weather I have 0 checked, all, or any combination. Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890817 Share on other sites More sharing options...
Psycho Posted August 4, 2009 Share Posted August 4, 2009 Where's your code (or post a link)? It should look something like this if you followed my code as an example: var noneChecked = true; var checkboxLen = form1['item[]'].length; for (var i=0; i<checkboxLen; i++) { if (form1['item[]'].checked) { oneChecked = false; break; } } if (noneChecked) { alert('Nothing checked.'); return false; } Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890846 Share on other sites More sharing options...
doby48 Posted August 4, 2009 Author Share Posted August 4, 2009 Exact code I am using is: var noneChecked = true; var checkboxLen = form1['item[]'].length; for (var i=0; i<checkboxLen; i++) { if (form1['item[]'][i].checked) { oneChecked = false; break; } } if (noneChecked) { alert('Nothing checked.'); return (false); } Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890851 Share on other sites More sharing options...
Psycho Posted August 4, 2009 Share Posted August 4, 2009 Well, there's your problem. You modified my code perfectly and didn't take into account that I make mistakes (note disclaimer in my sig) This line oneChecked = false; Should be (notice the 'n') noneChecked = false; Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890861 Share on other sites More sharing options...
doby48 Posted August 4, 2009 Author Share Posted August 4, 2009 Doh! Ya know, I even looked at that when I was adding in the echos, etc and thought it wasnt right and I needed to change it and I apparently forgot to go back and do that. Thank you very much, its working perfectly now. Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-890873 Share on other sites More sharing options...
zanmato Posted March 3, 2010 Share Posted March 3, 2010 Well I know this is an old thread but I'm posting anyways... I'm using exactly the same script as posted above, though the solution you provided that worked for the original poster doesn't work for me, I'm still having the same problem. Form (checkboxes): <input type="checkbox" id="platform_ps" name="platform[]" value="PS 1" /> PS1<br/> <input type="checkbox" id="platform_ps2" name="platform[]" value="PS 2" /> PS2<br/> <input type="checkbox" id="platform_ps3" name="platform[]" value="PS 3" /> PS3<br/> Javascript: var noneChecked = true; var checkboxLen = form1['platform[]'].length; for (var i=0; i->checkboxLen; i++) { if (form1['platform[]'].checked) { noneChecked = false; break; } } if (noneChecked) { error_msg += "- No platform was specified \n"; error = 1; } if (error ==1) { window.alert(error_msg); return false; } I also tried using the original alert message but this did not change anything, anyone knows what I'm doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-1020792 Share on other sites More sharing options...
Dennis1986 Posted March 3, 2010 Share Posted March 3, 2010 @ zanmato Is your form also named "form1" ? Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-1020796 Share on other sites More sharing options...
zanmato Posted March 3, 2010 Share Posted March 3, 2010 Yes it is, thanks for the suggestion Problem already solved tho if (form1['platform[]'].checked) { to if (form1['platform[]'][i].checked) { Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-1020812 Share on other sites More sharing options...
Psycho Posted March 3, 2010 Share Posted March 3, 2010 It's not the same code. Look at the second parameter in this for loop for (var i=0; i->checkboxLen; i++) { That should be i<checkboxLen Also, posting in an old thread, especially a solved one, is not a good idea. Start a new thread and link back tothe original if it is pertinent EDIT: This line is also incorrect: if (form1['platform[]'].checked) { Take a look at the originally provided code to see the problem. Quote Link to comment https://forums.phpfreaks.com/topic/168751-solved-verify-checkboxes-in-form/#findComment-1020880 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.