7 Replies Latest reply on Nov 28, 2006 10:00 AM by sbryner

    Save to XML

    sbryner Level 1
      Hello,

      Do you need to use php to save text entered from an input field into an XML file?

      I'd like to create a bunch of input boxes that "onRelease" of the save button saves each
      input into a specific order in an XML file so I can later call the file again and the childNodes would stay consistant each time I save.

      example:

      fName = myXML.firstChild.childNodes[ i ].firstchild.nodeValue

      this way I can continue to populate my XML file from inside a flash application.
      possible without php? guidance welcome.

      thanks,

      sky
        • 1. Re: Save to XML
          abeall Level 3
          > Do you need to use php to save text entered from an input field into an XML file?
          Yes, or some other third party.

          However, it kind of sounds like you don't need to change the XML outside of Flash? If that is the case, then yes, you can just change the existing nodes on you XML object. That will stay consistent within Flash, but it won't change the original file--meaning if you reload(which will happen if you close and re-open) you're back where you started.

          One alternative, depending on what your goal is, is to use the SharedObject to store local data for individual users. Other users(including you) can't access that data, of course, since its stored locally on the users machine, but it does provide a nice way to remember a user's experience across sessions.

          HTH
          • 2. Re: Save to XML
            sbryner Level 1
            I'm actually trying to save the file onto the server like an update.
            Scenario:

            you want to post something to my site. you fill out the form then hit send.
            then some code.... I don't know which... php, flash AS ??? will then add it
            to the "posted.xml" file on the server.

            I see that you can do this with php add to it using post. But I'm lost when it comes to posting input text from flash into an XML file on the server.
            on top of that I want each input field that is filled out to have a certain childNodes
            inside the "posted.xml" so that when anybody references the post it fills out the form and puts the first name in the name spot and the last name in the last name dynamic text box.

            example:

            <family>
            <person>
            <fname>tom</fname> // [ 0 ] [ 0 ]
            <lname>clancy</lname> // [ 0 ] [ 1 ] would be the reference for flash to read
            </person>

            // here is where the next person to be added to the xml file would go.
            <person>.........</person>

            // this information would be collected from a form and then saved here in the xml file on the server.

            </family>

            I'm just curious as to if this is possible and which scripting language would work best for this. I'm using it only to retrieve and store elements with childNodes in the xml file.

            any thoughts?
            • 3. Re: Save to XML
              abeall Level 3
              Yes, you will need to use some PHP or some other server-side scripting language.

              From the Flash side, you would probably send you variables using the LoadVars object(assuming you aren't using AS3) via the POST method, and then the PHP does the actual interpretation and writing to the file.

              my_lv = new LoadVars();
              my_lv.fname = fname_txt.text;
              my_lv.lname = lname_txt.text;
              my_lv.sendAndLoad("writeToXML.php", my_lv, "POST");

              (check out the docs on LoadVars)

              I don't know enough about PHP to give you reliable advice on how to approach that side of it.

              As far as loading the fname/lname, I'm not sure I followed where and how you would want that information to auto-populate. Would fname/lname be associated with a computer?

              • 4. Re: Save to XML
                sbryner Level 1
                thanks, Abeall.

                I'll look into the loadVars for this. The fName and lName would be entered by a client not a computer name.
                You could use it to look up a person in the xml file by first name, last name, id or what-have-you.
                The important things are that it saves to the xml file on the server and in the order that I want it too. ie( fName is always childNodes [ i ] [ 0 ] and lName is always saved as childNodes [ i ] [ 1 ] in the xml file.

                Thanks for giving me direction. I'll look into the loadVars and I need to research more on php and how php and xml are linked because if it saves to a php file and NOT my xml file will flash 8 AS2 still read it like it was an xml file?

                thanks again for the giving me an direction to research.
                sky
                • 5. Save to XML
                  abeall Level 3
                  > I'll look into the loadVars and I need to research more on php and how php and
                  > xml are linked because if it saves to a php file and NOT my xml file will flash 8 AS2
                  > still read it like it was an xml file?

                  You'll have two files, a PHP file which is basically a middle-man proxy, and an XML file. Since Flash can't save to an XML file, but PHP can, basically what you are doing is using LoadVars(or you could actually use the XML class in Flash which has send/sendAndLoad methods too) to hand off the information to the PHP, then the PHP actually writes to the file. So you might have 3 files that look like this:

                  yourXML.xml
                  saveToXML.php
                  yourFlash.swf

                  What you want to do is save information from yourFlash.swf to yourXML.xml. But, since Flash can't do this, what you have to do is send yourFlash.swf information to saveToXML.php with LoadVars or XML, then saveToXML.php will take that information and immediantly save it to the XML file.
                  • 6. Save to XML
                    abeall Level 3
                    Here is a small PHP script which is doing exactly what we are talking about. It takes POST data, opens an XML file, creates a new node with some attributes based on the POST data, and saves it to back to file. I'm also doing some checks to make sure the data is not too big or incorrectly formed, to prevent malicious attempts.

                    if(count($_POST) == 4
                    && strlen($_POST['fname']) <= 50
                    && strlen($_POST['lname']) <= 50
                    && strlen($_POST['email']) <= 50
                    && strlen($_POST['comment']) <= 250){

                    foreach ($_POST AS $key => $val)
                    $_POST[$key] = stripslashes($_POST[$key]);

                    $dom = new DomDocument();
                    $dom->load("guestbook.xml");
                    $node = $dom->createElement("comment");
                    //attr: fname
                    appendChildAttribute($dom,$node,"fname",$_POST['fname']);
                    //attr: lname
                    appendChildAttribute($dom,$node,"lname",$_POST['lname']);
                    //attr: email
                    appendChildAttribute($dom,$node,"email",$_POST['email']);

                    //append new node with comment text
                    $node->appendChild($dom->createTextNode($_POST['comment']));
                    $dom->documentElement->appendChild($node);

                    // Echo the output
                    //header('Content-type: text/xml');
                    //echo $dom->saveXML();

                    // Save to file
                    $dom->save("guestbook.xml");

                    }else{
                    echo "Invalid submission";
                    }

                    function appendChildAttribute($dom, $node, $attributeName, $attributeValue){
                    $attr = $dom->createAttribute($attributeName); //create attribute
                    $attr->appendChild($dom->createTextNode($attributeValue)); //assign attribute value
                    $node->appendChild($attr); //add the attribute to node
                    }
                    • 7. Re: Save to XML
                      sbryner Level 1
                      WOW, ok, I'm definately going to have to chew on this one for a bit. Thank you for providing me the code. At least now, I can go line by line and figure it then modify it to my needs.

                      can you check my brief breakdown on this...

                      1:
                      ( if statment) // checks to make sure the string length of each $_POST (variable) so no larger than the number on the right (50, 50, 50 or 250) correct?

                      2:
                      foreach ($_POST AS $key => $val) // don't understand... sets $_POST as a variable $key? or is $key to fetch a key from an associative array?

                      3:
                      $dom = new DomDocument();
                      // creates new DomDocument
                      $dom->load("guestbook.xml");
                      // loads the xml file into it.
                      $node = $dom->createElement("comment");
                      // creates an childNodes element within the DomDocument named("comment")
                      appendChildAttribute($dom,$node,"fname",$_POST['fname']);
                      // adds an attribute to the dom inside the node "fname" variable

                      wait a minute... I'm going to need some time to research all this script.

                      again,
                      thank you for the help.

                      sky

                      thanks so much,

                      sky