www.webdeveloper.com
Results 1 to 4 of 4

Thread: Assign Radio Buttons to Variables then window.location

  1. #1
    Join Date
    May 2014
    Posts
    7

    Assign Radio Buttons to Variables then window.location

    I have two different groups of radio buttons inside of a form.
    When the submit button is clicked I would like to pass the "value" of the radio buttons to variables, then use the variables to call a new page via window.location

    Sample Form

    <form>
    <Input type = 'Radio' Name ='firstp' value= 'starturl'>1
    <Input type = 'Radio' Name ='firstp' value= 'starturl1'>2
    <Input type = 'Radio' Name ='firstp' value= 'starturl2'>3

    <br

    <Input type = 'Radio' Name ='secondp' value= 'endurl'>1
    <Input type = 'Radio' Name ='secondp' value= 'endurl1'>2
    <Input type = 'Radio' Name ='secondp' value= 'endurl2'>3

    <input type="button" value="New Page" style="height: 40px; width: 180px; font-size: 17px;" onclick="move()" />
    </form>


    JAVASCRIPT(or lack there of)

    <script language="JavaScript">
    function move() {

    var beginURL = value from radio firstp
    var endURL = value from radio secondp


    window.location = beginURL + endURL
    }
    //-->
    </script>


    Of course I am new to javascript and I am just trying to explain in text what I am looking to do with this javascript code above, as I am little confused at this point.

    I will have multiple pages created that will have the exact names as the beginURL + endURL.
    Example if 2nd radio box was selected in both sections this page would load page starturl1endurl1.php

    Also thoughts on whether Javascript or PHP is best suited for this?
    Thanks

  2. #2
    Join Date
    Dec 2002
    Location
    Seattle, WA
    Posts
    1,843
    This is a simple task, JavaScript is enough. I would suggest not using the name move for a function. There is a global function with the same name window.move().
    Code:
    function radioValue(name)
    {
        var group = document.getElementsByName(name);
    
        if(group !== null) {
            for (var i = 0, l = group.length; i < l; ++i) {
                if (group[i].checked)
                    return group[i].value;
            }
        }
        return false;
    }
    function goto()
    {
        var val1 = radioValue('firstp'),
            val2 = radioValue('secondp');
    
        if (val1 === false || val2 === false)
            return;
        // build a valid URL ex. "./webpage.php"
        window.location.href = './' + val1 + val2 + '.php';
    }

  3. #3
    Join Date
    May 2014
    Posts
    683
    Uhm... this might be a REALLY stupid question... but why are you wasting time adding javascript to what should be a simple FORM[get]?!?

    Are we forgetting the unwritten rule of JavaScript or something? "If you can't make the page work without JavaScript FIRST, you likely have no business adding JavaScript to it."

  4. #4
    Join Date
    May 2014
    Posts
    7
    Thank you very much, worked perfectly.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles