Looping non-array variables with eval()

Hey. I want to do a for loop that grabs values from different variables. This function works nicely without the eval() statement (using the commented line below it) but not with it. What am I doing wrong? How could I make this work? Any help is much appreciated.

function saveData ()
{
saveUrl = “savedata.php?”;
for (count=1; count<=numberOfVariables; count++)
{
if (count>1) saveUrl += “&”;
saveUrl += “var” + count + “=” + eval(“var”+count);
//saveUrl += “var” + count + “=” + “blah”;
}
loadVariablesNum (saveUrl, 0, “GET”);
}

I don’t get what you are asking. You say “it works fine without the eval()”, then what’s the problem ??

well, i need to somehow get the value of each variable (the alternate line just uses the text ‘blah’), which is why i need the eval statement. unfortunately, putting it in there makes the loop not run properly (it seems to stop after the first iteration)

[AS]saveUrl += “var”+count+"="+“var”+count[/AS]

That will output var3=var3 though. Is that what you wanted ? I’m confused …

Well, that’s not exactly what I wanted…
What I’d like to do is send all these variables’ values to the php script. That means that if the value of var1 is “cat”, var2 is “dog” and var3 is “bird”, then the url should read “savedata.php?var1=cat&var2=dog&var3=bird”.

Will the amount of variables always be changing? Or will there always be a fixed amount?

Ah right, I see now =) You can store them in an array, like this:

[AS]vars = [“cat”,“dog”,“zebra”,“penguin”,“horse”,“lion”,“ant”]
function saveData() {
saveUrl = “savedata.php?”;
for (count=1; count<=vars.length; count++) {
if (count>1) {
saveUrl += “&”;
}
saveUrl += “var” + count + “=” + vars[count];
}
loadVariablesNum (saveUrl, 0, “GET”);
}[/AS]

Voetsjoeba: The title being “Looping <B>non-array</B> variables with eval()” I am assuming he doesn’t want to use arrays. I could be wrong.

[AS]var1 = “cat”;
var2 = “dog”;
var3 = “bird”;
function saveData() {
saveUrl = “savedata.php?”;
for (var i = 1; i<=3; i++) {
if (i>1) {
saveUrl += “&”;
}
saveUrl += “var”+i+"="+this[“var”+i];
}
loadVariablesNum(saveUrl, 0, “GET”);
}[/AS]

This works, well… with a set of 3 variables… to make it a dynamic amount of variables may require more work, depending on how exact these variables are being set.

*Originally posted by lostinbeta *
**Voetsjoeba: The title being “Looping <B>non-array</B> variables with eval()” I am assuming he doesn’t want to use arrays. I could be wrong. **

Lol, missed that :stuck_out_tongue:

funny. if i add the three lines that set the variables to dog cat and bird, it makes my script work, which is odd because the textboxes are in the main timeline, where the script is, so if i have values typed in, that should work the same way, no?

by the way, thanks for your help, people.

ok… this might give a hint. in the textbox that displays the resulting string, saveUrl, the text is set to be red. But the value calculated for the first this[] or eval[] term is always black for some reason, and that’s where it cuts off. could i have buggy software or something?

Well & being loaded into Flash said “new variable” so it stops reading the information after that point.

Soooo, what you could do is escape() the & symbol so that it will return formatted version of it (%26)

Try something like this…

[AS]function saveData() {
saveUrl = “savedata.php?”;
for (var i = 1; i<=3; i++) {
if (i>1) {
saveUrl += escape("&");
}
saveUrl += “var”+i+"="+this[“var”+i];
}
loadVariablesNum(saveUrl, 0, “GET”);
}[/AS]

no dice… i commented out that entire line (the one with the if statement) and it gave the same effect - the first value appended (shown in a different color), with nothing else after it

Can you supply files so we can test things out on our side and see whats up?

ok. i don’t know how to post a file here, so here’s a link. you can view the swf or download the fla. by the way, are flash source files compatible between mac and pc versions?

the link: http://www.embroidirect.com/temp/

Why did you change the extension to txt ? Anyways … The problem is the HTML render option. I disabled it and it worked again =)