Using a hyphen in a post variable using LoadVars
If you’ve ever tried to use LoadVars to send a form that requires a hyphen in the variable name, you might be pulling your hair out right now. For example, lets say you’re trying to submit a form variable like this:
loginin-id=15
So, in Flash, you probably tried something like this:
var loadVarsSender:LoadVars = new LoadVars();
loadVarsSender.login-id="Some data";
var loadVarsReceiver:LoadVars = new LoadVars();
loadVarsSender.sendAndLoad("somepage.php", loadVarsReceiver, "POST");
But the problem is that Flash sees that as login minus id. It’s treating the hyphen as a subtraction operator. The solution, wrap the variable name in square braces like so:
var loadVarsSender:LoadVars = new LoadVars();
loadVarsSender["login-id"]="Some data";
var loadVarsReceiver:LoadVars = new LoadVars();
loadVarsSender.sendAndLoad("somepage.php", loadVarsReceiver, "POST");
That’ll tell flash that the hyphen is part of the variable name. This should work for any other strange characters that you need to use in the variable name too.
loadVarsSender.sendAndLoad(“somepage.php”, loadVarsReceiver, “POST”);
