Jump to content

Auto fill check boxes in a form


ghgarcia

Recommended Posts

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
Link to comment
Share on other sites

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]
Link to comment
Share on other sites

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,
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.