3 Replies Latest reply on Dec 2, 2006 9:08 AM by thespark

    problem with sending xml to php with URLRequest

    thespark Level 1
      Okeay if someone could tell me what is I am doing wrong? I get the following errors when I try to send xml file to a php and write it to xml file: Error #2044: Unhandled ioError:. text=Error #2032: Stream Error.

      And my code is following:

      Main app:

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" creationComplete="LoadXML()" layout="absolute">
      <mx:Script source="VieraskirjaScripti.as"/>
      <mx:DataGrid x="10" y="10" valueCommit="sort(event)" id="mygrid" width="245" height="312" variableRowHeight="true">
      <mx:columns>
      <mx:DataGridColumn headerText="Vieraskijan merkinnät" textAlign="center" fontFamily="Georgia" fontSize="15" fontWeight="bold" dataField="col1">
      <mx:itemRenderer>
      <mx:Component>
      <mx:VBox>
      <mx:Text fontFamily="Georgia" fontSize="12" fontWeight="normal" textAlign="right" text="{data.Nimi} kirjoitti:"/>
      <mx:Text fontFamily="Georgia" fontSize="12" fontWeight="normal" textAlign="right" text="{data.Viesti}"/>
      <mx:Image source="{ data.Kuva }"/>
      </mx:VBox>
      </mx:Component>
      </mx:itemRenderer>
      </mx:DataGridColumn>
      </mx:columns>
      </mx:DataGrid>
      <mx:Button x="201" y="348" id="Uusi" click="LisaaUusi()" label="Button"/>
      <mx:Button x="298" y="348" label="save" click="SaveXML(); valmis.text = str"/>
      <mx:TextArea x="263" y="10" height="312" width="194" id="valmis"/>
      </mx:Application>

      VieraskirjaScripti.as:

      // ActionScript file
      import mx.collections.XMLListCollection;
      import mx.collections.Sort;
      import mx.collections.SortField;
      import flash.net.*;
      import flash.events.Event;
      private var str:String;
      [Bindable]
      private var myXML:XML = new XML();

      private function LoadXML():void{
      var XML_URL:String = "Vieraskirja.xml";
      var myXMLURL:URLRequest = new URLRequest(XML_URL);
      //myXMLURL.method = URLRequestMethod.POST;
      //myXMLURL.data = myXML
      var myLoader:URLLoader = new URLLoader(myXMLURL);
      myLoader.addEventListener("complete", xmlLoaded);
      }

      private function xmlLoaded(evtObj:Event):void {
      var loader:URLLoader = URLLoader(evtObj.target);
      myXML = XML(loader.data);
      trace(myXML.elements('Merkinta'));
      mygrid.dataProvider = myXML.elements('Merkinta');
      sort(evtObj);
      }

      private function SaveXML():void{
      var PHP_URL:String = "Vieraskirja.PHP";
      var myPHPURL:URLRequest = new URLRequest(PHP_URL);
      myPHPURL.method = URLRequestMethod.POST;
      myPHPURL.contentType = "text/xml";//"application/xml";
      myPHPURL.data = myXML;
      var mySaver:URLLoader = new URLLoader(myPHPURL);
      mySaver.addEventListener("complete", phpSaved);
      }
      private function phpSaved(evtObj:Event):void{
      var saver:URLLoader = URLLoader(evtObj.target);
      str = saver.data;
      }
      private function LisaaUusi():void{
      var mychild:XML = <Merkinta>
      <Nimi>Pasi</Nimi>
      <Viesti>123456789Pasi987654321</Viesti>
      <Kuva>Hymio02.gif</Kuva>
      <Indeksi>9</Indeksi>
      </Merkinta>
      myXML = myXML.appendChild(mychild);
      trace(mygrid.dataProvider);
      mygrid.dataProvider = myXML.elements('Merkinta');
      }

      private function sort(event:Event):void{
      if(!(mygrid.dataProvider == null)){
      var ac:XMLListCollection = XMLListCollection(mygrid.dataProvider);
      trace(ac);
      var sort:Sort = new Sort();
      sort.fields=[new SortField("Indeksi",true,true,true)];
      ac.sort = sort;
      ac.refresh();
      event.preventDefault();
      }
      }

      nad my php file:

      <?php
      $file = fopen("Vieraskirja.xml", "w+") or die("Can't open XML file");
      $xmlString = $HTTP_RAW_POST_DATA;
      if(!fwrite($file, $xmlString)){
      print "Error writing to XML-file";
      }
      print $xmlString."\n";
      fclose($file);
      ?>

      I just can't figure this out

      EDiT: my test Vieraskirja.xml looks like the following:

      <?xml version="1.0"?>
      <Vieraskirja>
      <Merkinta>
      <Nimi>Pasi</Nimi>
      <Viesti>TestiViesti</Viesti>
      <Kuva>Hymio02.gif</Kuva>
      <Indeksi>1</Indeksi></Merkinta>
      <Merkinta>
      </Vieraskirja>