...

View Full Version : HELP! Javascript .split() String into Pairs of Variables



BigDork
09-04-2006, 06:08 PM
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:

<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:

<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:

(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.

:eek: I can't find my error, please help! :confused:

Mr J
09-04-2006, 07:04 PM
Something on these lines?



<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>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum