Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 12 of 12
  1. #1
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Radio buttons and universal variables

    I am able to send the values of my radio buttons to the next page, but it is the last value for each group of radio buttons. How do I change the code so that it takes the radio I select and not the last one in each group. The code is straight from a universal variable example and I have only changed a few things. I know it'll probably be a simple fix but I can't seem to put my finger on it.

    meeka007

    Page 1
    passbyname.html

    <BODY>
    <SCRIPT type=text/javascript>
    //<![CDATA[
    if (typeof(document.layers)!='undefined') alert("This script does not work in Netscape 4!");

    function gatherData(f){
    window.name="";
    for(i=0; i<f.length; i++)
    if(f[i].type == 'radio')
    window.name+=f[i].name+"='"+f[i].value+"';";
    location=f.action;
    }

    //]]>
    </SCRIPT>
    <CENTER>
    <FORM onsubmit="gatherData(this);return false" action=passedbyname.html method="post">
    <INPUT type="radio" value="work darn it" name=radio1>work darn it<BR>
    <INPUT type="radio" value="nope" name=radio1>text2value<BR>
    <input type="radio" value="crud" name=radio1>Wow<BR>

    <INPUT type="radio" value="work dang it" name=radio2>work darn it<BR>
    <INPUT type="radio" value="not again" name=radio2>text2value<BR>
    <input type="radio" value="crud" name=radio2>Wow<BR>

    <INPUT type=submit value=Submit>
    </CENTER>
    </FORM>
    </BODY>


    Page 2
    passedbyname.html

    <SCRIPT type=text/javascript>
    //<![CDATA[
    function parseWindowNameString()
    {
    var pairs=window.name.split("';");
    for (var i=0;i<pairs.length-1;i++){
    var pair = pairs[i].split("='");
    this[pair[0]]=pair[1];
    }
    }
    var passedData = new parseWindowNameString();

    //this will output all the passed variables
    document.write("These are the variables passed:<br />");
    for (o in passedData) document.write(o + " = " + passedData[o]+"<br />");
    //]]>

    </SCRIPT>

    <META content="Microsoft FrontPage 4.0" name=GENERATOR></HEAD>
    <BODY></BODY>

  • #2
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    The only thing you *needed* to change to fix this was to add a test for whether the element was checked. I went ahead and fixed this to both work in any browser and work with most any element value.

    passbyname.html:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    //<![CDATA[
    function gatherData (f) {
    	var el;
    	window.name = "";
    	for (i=0; i<f.elements.length; i++) {
    		el = f.elements[i];
    		if (el.type == 'radio' && el.checked) {
    			window.name += el.name + "='" + escape(el.value) + "';";
    		}
    	}
    	location=f.action;
    }
    //]]>
    </script>
    </head>
    <body>
    <form onsubmit="gatherData(this);return false" action="passedbyname.html" method="post">
    	<input type="radio" value="work darn it" name="radio1" />work darn it<br />
    	<input type="radio" value="nope" name="radio1" />text2value<br />
    	<input type="radio" value="crud" name="radio1" />Wow<br />
    	<input type="radio" value="work dang it" name="radio2" />work darn it<br />
    	<input type="radio" value="not again" name="radio2" />text2value<br />
    	<input type="radio" value="crud" name="radio2" />Wow<br />
    	<input type="submit" value="submit" />
    </form>
    </body>
    </html>


    passedbyname.html:
    Code:
    <html>
    <head>
    <script type=text/javascript>
    //<![CDATA[
    function parseWindowNameString () {
    	var pair,
    		pairs=window.name.split("';");
    	for (var i=0;i<pairs.length-1;i++){
    		pair = pairs[i].split("='");
    		this[pair[0]] = unescape(pair[1]);
    	}
    }
    var passedData = new parseWindowNameString();
    //this will output all the passed variables
    document.writeln("These are the variables passed:<br />");
    for (o in passedData) {
    	document.writeln(o + " = " + passedData[o]+"<br />");
    }
    //]]>
    </script>
    </head>
    <body></body>
    </html>

  • #3
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thank you and code explinations

    Thanks a lot for your reply Choopernickel. I didn't think I'd get such a quick response. I was wondering if you could explain what it was that you added and how it works. I was able to get it to work but I'm not totally sure how it works. I knew I had to do something with the && to get it to read the radio button and the value but couldn't figure it out. Any comments would be greatly appreciated. Thanks again.

    meeka007

  • #4
    Regular Coder
    Join Date
    Apr 2003
    Location
    Atlanta, GA
    Posts
    487
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Sure thing. Annotations are in red.

    passbyname.html:
    Code:
    <html>
    <head>
    <script type="text/javascript">
    //<![CDATA[
    function gatherData (f) {
    	var el; // this variable will hold the current element in the loop 
    	window.name = "";
    	for (i=0; i<f.elements.length; i++) { // refer to form.elements here, not just form
    	// since all browsers have a form.elements object
    		el = f.elements[i]; // hold the current element in the temp variable
    		if (el.type == 'radio' && el.checked) { // see that && el.checked? It tells you 
    		// whether the element we're looking at is... checked
    			// escape() will encode a value to keep it from containing any illegal characters
    			window.name += el.name + "='" + escape(el.value) + "';"; 
    		}
    	}
    	location=f.action;
    }
    //]]>
    </script>
    </head>
    <body>
    <!-- all I did in the HTML was clean it up and remove presentational stuff -->
    <form onsubmit="gatherData(this);return false" action="passedbyname.html" method="post">
    	<input type="radio" value="work darn it" name="radio1" />work darn it<br />
    	<input type="radio" value="nope" name="radio1" />text2value<br />
    	<input type="radio" value="crud" name="radio1" />Wow<br />
    	<input type="radio" value="work dang it" name="radio2" />work darn it<br />
    	<input type="radio" value="not again" name="radio2" />text2value<br />
    	<input type="radio" value="crud" name="radio2" />Wow<br />
    	<input type="submit" value="submit" />
    </form>
    </body>
    </html>


    passedbyname.html:
    Code:
    <html>
    <head>
    <!-- I moved the function into the head of the document-->
    <script type=text/javascript>
    //<![CDATA[
    function parseWindowNameString () {
    	var pair, // You only need the var keyword the first time you create a variable
    		// Also, the comma after pair continues the 
    		// var declarations to the next line (or string)
    		pairs=window.name.split("';");
    	for (var i=0;i<pairs.length-1;i++){
    		pair = pairs[i].split("='");
    		this[pair[0]] = unescape(pair[1]); // unescape() undoes the escape() call from earlier
    	}
    }
    var passedData = new parseWindowNameString();
    //this will output all the passed variables
    document.writeln("These are the variables passed:<br />");
    // document.writeln() is safer to the document than document.write()
    for (o in passedData) {
    	document.writeln(o + " = " + passedData[o]+"<br />");
    }
    //]]>
    </script>
    </head>
    <body></body>
    </html>

  • #5
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks, that helps a lot. The explinations sure do help understanding it better. Have a good one and happy coding.

    meeka007

  • #6
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts

    getting the variable

    Now I am having a problem with the data after it has been passed. I want to set a variable on the next page to the value that I passed. How do I accomplish that, does it involve global variables, or am I way off? One other semi-related question, can I reference variables from one funtion to another or how do I do that? Thanks

    -meeka007

  • #7
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    When you do this:

    var passedData = new parseWindowNameString();

    the data are now stored in the passedData object.
    To access the data from the previous page, just specify the element name:

    alert(passedData['radio1'])
    alert(passedData['radio2'])

    passedData is a global variable so you can access it anywhere.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #8
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Thanks Glenn. I somehow found a way to get it to work but this way looks a lot easier and less confusing. Thanks again for the reply.

    -meeka007

  • #9
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    Can you tell us what you did that's a lot easier, for the sake of other members that might be using that script?
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #10
    Senior Coder
    Join Date
    Aug 2002
    Location
    Kansas City, Kansas
    Posts
    1,518
    Thanks
    0
    Thanked 2 Times in 2 Posts
    I somehow found a way to get it to work but this way looks a lot easier and less confusing.
    I think that he means that your way was easier Glenn.

  • #11
    Supreme Master coder! glenngv's Avatar
    Join Date
    Jun 2002
    Location
    Philippines
    Posts
    11,068
    Thanks
    0
    Thanked 256 Times in 252 Posts
    A ok. I got confused there. I thought he said he somehow found other way that looks easier.
    Glenn
    ____________________________________

    My Blog
    Tower of Hanoi Android app (FREE!)
    Tower of Hanoi Leaderboard
    Samegame Facebook App
    vBulletin Plugins
    ____________________________________

  • #12
    New Coder
    Join Date
    Jan 2004
    Location
    Minnesota
    Posts
    10
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You are correct Antoniohawk. My way is utter confusion and I don't know how I got it to work, glenngv's way makes sense and works. Thanks again for the reply. One quick unrelated question, why does the envelope for this post on the forum page have a black dot on it and no one else's does?

    -meeka007
    Last edited by meeka007; 02-11-2004 at 02:32 PM.


  •  

    Posting Permissions

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