Link to home
Start Free TrialLog in
Avatar of OBCT
OBCT

asked on

Dynamically loading data into the drop down box

I have a UI componant (combo box) and I want to be able to load the list from a text file.
Is this possible? If so, how?
The other thing I need is to be able to send the selection from the combo box into a mysql database. I have set up a php page that sends the data from my text boxes to the database, so really all I need is to know how to send it to php.
I hope this makes sense, if not I'll try and explain better. Just let me know.

Cheers

-OBCT
Avatar of negatyve
negatyve

Create a "combodata.xml" xml file, and write in it:

<?xml version="1.0" encoding="UTF-8"?>
<combodata>
      <item label="First Label"      data="first" />
      <item label="Second Label"      data="second" />
      <item label="Third Label"      data="third" />
      <item label="Fourth Label"      data="fourth" />
      <item label="Fifth Label"      data="fifth" />
      <item label="Sixth Label"      data="sixth" />
</combodata>

then, in your movie, call your combobox, for example, "combo_cb", and drop this code on the same frame:

combo_cb.setEnabled(false);
data_xml = new XML();
data_xml.ignoreWhite = true;
data_xml.path = this;
data_xml.onLoad = function(ok)
{
      this.path.combo_cb.setEnabled(true);
      if(ok){
            var nodes = this.firstChild.childNodes;
            for(var i = 0; i < nodes.length; i++){
                  this.path.combo_cb.addItem(nodes[i].attributes.label, nodes[i].attributes.data);
            }
            this.path.combo_cb.setChangeHandler("changeData");
            this.path.changeData(combo_cb);
      } else {
            this.path.combo_cb.addItem("no data found");
            this.path.combo_cb.setEnabled(false);
      }
};
function changeData(_cb)
{
      this.yourVariable = _cb.getSelectedItem().data;
}
data_xml.load("combodata.xml");



with this code, when you change the selected item, the "yourVariable" variable is created in the same timeline where the combo lies, holding the selected item data.
If you want to create it in a loadVars instance, change the code to:

data_var = new LoadVars();

combo_cb.setEnabled(false);
data_xml = new XML();
data_xml.ignoreWhite = true;
data_xml.path = this;
data_xml.onLoad = function(ok)
{
      this.path.combo_cb.setEnabled(true);
      if(ok){
            var nodes = this.firstChild.childNodes;
            for(var i = 0; i < nodes.length; i++){
                  this.path.combo_cb.addItem(nodes[i].attributes.label, nodes[i].attributes.data);
            }
            this.path.combo_cb.setChangeHandler("changeData");
            this.path.changeData(combo_cb);
      } else {
            this.path.combo_cb.addItem("no data found");
            this.path.combo_cb.setEnabled(false);
      }
};
function changeData(_cb)
{
      data_var.yourVariable = _cb.getSelectedItem().data;
}
data_xml.load("combodata.xml");


Now, when you send you data to php (you know how, if you send data from textfields), you get that variable with:

$_POST['yourVariable']
Avatar of OBCT

ASKER

Ok I've done that and its working fine.
I just have to more questions....

1) How can I adjust the width of the combo box?

2) I was intending on using LoadVars to send the data to my php page seeing the rest of my data is sent that way, but....
how to I get flash to get the variable the user has selected?  e.g. a text box uses      newVars.txt_1 = txt_1.text       so what would/should I use for the combo box?

Cheers

-OBCT
Avatar of rascalpants
your combo box will automatically adjust to the max item size...  so you don't have to worry about that...

you can use loadvars if you want...

you would just send that information in the changeData function above....  like this:

function changeData(_cb)
{
    myData = new LoadVars();
    myData.yourVariable = _cb.getSelectedItem().data;
    myData.sendAndLoad("/yourpage.php", myData, "POST");
    myData.onLoad = function(success){
        if(success){
           trace("successful transaction");
        } else {
           trace("an error occured in the transaction");
        }
    };
}


I believe, that is all you need...  then you would collect the "yourVariable"  using the method posted above by Negatyve....


hope that helps to clarify...


rp
Avatar of OBCT

ASKER

I tried adding the loadvars in my on release function, but now I'm having three problems.
1) The username and email are not being submited to my db anymore
2) The combo box didn't adjust its width automatically
3) In the database, in the column that contains my variable from the combo box, it says undefiend.

What did I do wrong??
Avatar of OBCT

ASKER

Sorry about those errors, they were my own little errors which I have fixed up now.
However, in my database I am still getting "undefiend" in the column that is meant to hold the value from my text box.
it should be:

$_POST['textfield_associated_variable']
Avatar of OBCT

ASKER

This is the code I am using, and it wont send anything into my database. The way I had it working before was using loadVariablesNum with the GET method...
any suggestions?

combo_cb.setEnabled(false);
data_xml = new XML();
data_xml.ignoreWhite = true;
data_xml.path = this;
data_xml.onLoad = function(ok)
{
     this.path.combo_cb.setEnabled(true);
     if(ok){
          var nodes = this.firstChild.childNodes;
          for(var i = 0; i < nodes.length; i++){
               this.path.combo_cb.addItem(nodes[i].attributes.label, nodes[i].attributes.data);
          }
          this.path.combo_cb.setChangeHandler("changeData");
          this.path.changeData(combo_cb);
     } else {
          this.path.combo_cb.addItem("no data found");
          this.path.combo_cb.setEnabled(false);
     }
};
function changeData(_cb)
{
     this.newUser = _cb.getSelectedItem().data;
}
data_xml.load("http://www.dfgsgsdfgsdfg.com/xml/combodata.xml");

submitBTN.onRelease = function() {
      if (!username.length) {
            status = "Please enter a username"
      } else {
            if (!email.length or email.indexOf("@") == -1 or email.indexOf(".") == -1 or email.indexOf("com") == -1) {
                  status = "Please enter a valid email"
            } else {
                  if (email.length < 7) {
                        status = "Please enter a valid email"
                  } else {
                        status = "Loading...Please wait..."
                        var newUser = new LoadVars()
                        newUser.username = _root.username
                        newUser.email = _root.email
                        newUser.timeZone = _root._cb.getSelectedItem().data
                        newUser.load("http://www.dfgsfgsdfg.com/scripts/register.php", 0, "POST")
                        newUser.onLoad = function(success) {
                              if (success) {
                                    status = "Complete"
                              } else {
                                    status = "Error"
                              }
                        }
                  }
            }
      }
}
Avatar of OBCT

ASKER

Can someone please help!
ASKER CERTIFIED SOLUTION
Avatar of negatyve
negatyve

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of OBCT

ASKER

I tried that, used $HTTP_POST_VARS on my php script and it didn't add anything into the database.
Avatar of OBCT

ASKER

I know there isn't anything wrong with my php page because when I try something like this:
http://www.sdlfhs.com/scripts/register.php?username=Blah&email=i_live@themoon.com
it enters it into the db.
Avatar of OBCT

ASKER

LOL sorry my bad. I forgot to save my php script after I changed everything to HTTP_POST_VARS.
Thanks for all your help, you really are an expert!

Cheers!

-OBCT
yeah!! :)

cheers