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 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Sep 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Exclamation HELP! Javascript .split() String into Pairs of Variables

    I apologize if this is a no-brainer, but I'm fairly new to this stuff. I'll try to keep this simple.

    The URL is similar to:
    https://url.com/form.htm?string=p,val1*l,val2*m,val3*t,val4*d,val5

    Within the form, I have the following statement:
    Code:
    <input type="hidden" name="string">
    With this statement, string should take the value "p,val1*l,val2*m,val3*t,val4*d,val5"

    I'm having a problem with the following script:
    Code:
    <script>
    var stuff=document.form_name.string.value;
    function BreakPieces()
    {	
    	var pieces=stuff.split('*');
    	for (var num=0;   num<pairs.length;   num++)
    	{	
    		var piece=pieces[num].split(',');
    		this[piece[0]]=piece[1];
    	}
    }
    var order=new BreakPieces();
    </script>
    The array order remains undefined.

    I need this script to produce order["p"]=val1, order["l"]=val2, order["m"=val3, etc...

    Here is a script that I use on other pages to derive these variables from the URL.
    Unfortunately, the new page that I need this new script for uses a CGI script to validate the form, and it can't carry the URL variables.
    I can initially pass them to the new page as string, which populates a hidden form element, but I need them broken out again to the order array for some other functions on the same page.

    Here is the script that I'm using on previous pages without problem:
    Code:
    (With a URL of http://url.com/page.htm?=p=val1&l=val2&m=val3&t=val4&d=val5)
    
    <script>
    function BreakURLpieces()
    {	
    	var URLpieces=unescape(location.search.substring(1).replace(/\+/g," ")).split('&');
    	for (var num=0;   num<pairs.length;   num++)
    	{
    		var URLpiece = URLpieces[num].split('=');
    		this[URLpiece[0]]=URLpiece[1];
    	}
    }
    var order=new BreakURLPieces();
    </script>
    This script works on other pages without a hitch. I'm just trying to modify it to read the variable stuff instead of the URL.

    I can't find my error, please help!

  • #2
    Senior Coder
    Join Date
    Aug 2002
    Location
    UK
    Posts
    2,789
    Thanks
    2
    Thanked 14 Times in 14 Posts
    Something on these lines?


    PHP Code:
    <div id="d1"></div>

    <
    script type="text/javascript">

    var 
    str="https://url.com/form.htm?string=p,val1*l,val2*m,val3*t,val4*d,val5"

    function BreakPieces(){    

    var 
    split1=str.split('=');

    split2=split1[1].split("*")

    for(var 
    i=0;i<split2.length;i++){

    split3=split2[i].split(",")

    document.getElementById("d1").innerHTML+="order[\""+split3[0]+"\"] = "+split3[1]+"<br>"

    }
        
    }

    var 
    order=new BreakPieces()

    </script> 
    The silent one.

    The most dangerous thing in the world is an idea.
    The most dangerous person in the world is the one with an idea.


  •  

    Posting Permissions

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