SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    tx
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    radio button help

    Hi all, I have a search form. It has 2 radio buttons - 'all words' and 'any words'. The 'all words' is checked when the page is loaded. If the user checks the second radio button, then searches, on the results page (the search inputs stay at the top of the page as the form submits to itself), the checked radio button goes back to its checked default of 'all words'. Is there any way of making the checked button stay on the button the user clicked when going to the results page? Hope this makes sense.

    <input type='radio' name='mode' value='allwords' CHECKED>
    <input type='radio' name='mode' value=anywords'>

  2. #2
    ☆★☆★ silver trophy vgarcia's Avatar
    Join Date
    Jan 2002
    Location
    in transition
    Posts
    21,235
    Mentioned
    1 Post(s)
    Tagged
    1 Thread(s)
    Is the form's method GET or POST? If it's GET, you can change which radio button is checked on page load with Javascript. If it's POST, you'll have to set the value with a server-side language like ASP.

    This should choose the proper radio button choice if the form's method is GET:
    Code:
    function checkChosenRadio()
    {
      //variable to store radio buttons
      var radios = document.getElementsByName("mode");
      //get the query string
      var QS = location.search;
      //check to see if the radio button's name and value is
      //in the query string using a regexp
      var RE = /mode=(all|any)words/gi, i = 0, thisRadio;
      if (RE.exec(QS))
      {
         while (thisRadio = radios[i++])
          {
             if (thisRadio.value.indexOf(RE.$1) != -1)
             {
                thisRadio.checked = true;
             }
          }
      }
    }
    window.onload = checkChosenRadio;

  3. #3
    SitePoint Enthusiast
    Join Date
    Feb 2004
    Location
    tx
    Posts
    40
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks for the reply. id posted also in the asp forum, cos i wasnt sure which route to take. looks like i fixed it in asp with the help of one of you guys there. Thanks.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •