ghgarcia Posted December 4, 2006 Share Posted December 4, 2006 have a football pool and I want to add to the weekly picks for my users the option of pressing a single button and having all visiting or home teams selected another option would be a random selection of picks. I have it half way working however it doesn't set the correct picks as checked. You can check out what I've done by going to [url=http://lvbash.com/phpfootball20/entryx.php]http://lvbash.com/phpfootball20/entryx.php?week=14[/url]. Please don't press the Submit button since this script won't update the database. This is the code that I use:[quote]function RandomPicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio")) { var r=Math.round(Math.random())+1; PForm.elements[i-r].checked=true; } }function AwayPicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio")) (PForm.elements[i-2].checked=true) }function HomePicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio")) (PForm.elements[i-0].checked=true) }[/quote]George Quote Link to comment https://forums.phpfreaks.com/topic/29370-auto-fill-check-boxes-in-a-form/ Share on other sites More sharing options...
artacus Posted December 4, 2006 Share Posted December 4, 2006 TMC -- too much code.But its easy to have PHP select the correct boxes if by adding "SELECTED" to the <input ...>To do it from Javascript you'll have to add something to identify which checkboxes are home or away.Perhaps the easiest method would be to store the id's of all the home and away check boxes in js arrays, then have your function go thru both arrays using getElementByID setting one to checked the other to unchecked.Another approach would be to use getElementsByTagName to go thru all your checkboxes and set the appropiate state. You could define a variable right in the input definition. [code]<input type='checkbox' id='whatever' is_home='1'>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/29370-auto-fill-check-boxes-in-a-form/#findComment-135029 Share on other sites More sharing options...
ghgarcia Posted December 5, 2006 Author Share Posted December 5, 2006 Your suggestion fixed my problem I added an id element to the input items and modified the javacode to look for the id. It works perfectly. Thanks for the help I really appreciate it.The following is my javascript:[quote]function RandomPicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio")) { var r=Math.round(Math.random())+1; PForm.elements[i-r].checked=true; } }function AwayPicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio" && PForm.elements[i].id=="visitor")) (PForm.elements[i-0].checked=true) }function HomePicks(PForm) { for (i=0; i < PForm.elements.length-1; i++) if((PForm.elements[i].type=="radio" && PForm.elements[i].id=="home")) (PForm.elements[i-0].checked=true) }[/quote]Thanks, Quote Link to comment https://forums.phpfreaks.com/topic/29370-auto-fill-check-boxes-in-a-form/#findComment-135276 Share on other sites More sharing options...
artacus Posted December 5, 2006 Share Posted December 5, 2006 I'm glad, you only have a few more weeks left in the season. Seems like every year around this time someone is contacting me to build some kind of football pool. It must be a pretty big "business" Quote Link to comment https://forums.phpfreaks.com/topic/29370-auto-fill-check-boxes-in-a-form/#findComment-135549 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.