11 Replies Latest reply on Jul 28, 2006 8:07 PM by Vision Airlines

    Refreshing Datagrid?

    Vision Airlines
      The following MXML script pulls values from a MySQL table by getting them through gettest.php which accesses MySQL and returns XML. You can also push new comments to the MySQL table. The problem is, even though i can verify that the added comments make it into MySQL, the datagrid doesn't update( even if I push "getit"). I know i must be missing something simple. Can anyone tell me why the following will not refresh the datagrid after submitting a new value to MySQL through the pushtest.php?

      <?xml version="1.0" encoding="utf-8"?>
      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" creationComplete="getit()">
      <mx:Script>
      <![CDATA[


      public function getit():void {
      gettest.send()
      }

      public function pushit():void {
      pushtest.send(addme)
      textinput.text=""
      gettest.send()

      }

      ]]>
      </mx:Script>
      <mx:HTTPService id="gettest" url=" http://www.mysite.com/gettest.php"/>
      <mx:HTTPService id="pushtest" url=" http://www.mysite.com/pushtest.php" useProxy="false" method="POST"/>

      <mx:Model id="addme">
      <myData>
      <comment>{textinput.text}</comment>
      </myData>
      </mx:Model>

      <mx:DataGrid id="datagrid" y="10" dataProvider="{gettest.lastResult.users.user}" left="10" right="10" height="454">
      <mx:columns>
      <mx:DataGridColumn headerText="id" dataField="id"/>
      <mx:DataGridColumn headerText="comment" dataField="comment"/>
      </mx:columns>
      </mx:DataGrid>
      <mx:TextInput id="textinput" y="472" height="24" width="595" x="10"/>
      <mx:Button x="613" y="472" label="Push" click="pushit()"/>
      <mx:Button x="675" y="472" label="Get" click="getit()"/>

      </mx:Application>
        • 1. Re: Refreshing Datagrid?
          peterent Level 2
          Let me see if I understand this. When you do gettest.send() the result that comes back is not showing up in the DataGrid?

          What I would do in that case is put a result handler on the gettest HTTPService and in that function look to see what is actually coming back. There may be another level of XML you need to specify or perhaps you are specifying too many.
          • 2. Refreshing Datagrid?
            Vision Airlines Level 1
            Thanks for the reply.

            quote:

            Originally posted by: peterent
            Let me see if I understand this. When you do gettest.send() the result that comes back is not showing up in the DataGrid?


            It shows up the first time. But, any changes after that do not update in the datagrid. If you close the browser and then open it again it pulls the updated data.

            quote:

            What I would do in that case is put a result handler on the gettest HTTPService and in that function look to see what is actually coming back. There may be another level of XML you need to specify or perhaps you are specifying too many.



            I will try that. In the mean time,in case you happen to see something obvious that I don't, the following is the php script from gettest.php:

            <?php
            //Connect to the database**********************************
            $conn = mysql_pconnect("mysql.host.com","username","password");
            mysql_select_db("testdb");

            //Queries**********************************
            //return a list of all the users
            $Query = "SELECT * FROM testtable";
            $Result = mysql_query($Query,$conn);

            //**********************************

            $Return = "<users>";

            while ( $User = mysql_fetch_object( $Result ) )
            {
            $id=$User->id;
            $comment=$User->comment;

            $Return .= "<user><id>".$id."</id><comment>".$comment."</comment></user>";
            }//while $Result
            $Return .= "</users>";
            mysql_free_result( $Result );
            print ($Return)

            ?>
            • 3. Refreshing Datagrid?
              Vision Airlines Level 1
              Ok, I used an item renderer and still the datagrid doesn't refresh. Since I'm not sending any values with the gettest.send(), is there some problem with the php page I'm accessing not refreshing or something?
              • 4. Re: Refreshing Datagrid?
                peterent Level 2
                As long as you can reliably call your PHP cript from a browser, not using Flex, and you see your XML result in the browser, then your PHP is fine.

                If you could either email me (pent@adobe.com) or post a bit of sample XML that would be a (small) typical response, I can work with it to see what may be the matter. I don't need the PHP.
                • 5. Re: Refreshing Datagrid?
                  Vision Airlines Level 1
                  I think the following new post described pretty much what's happening Peter.

                  http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?catid=585&threadid=1176382

                  Can you suggest a better way of solving this problem?
                  • 6. Re: Refreshing Datagrid?
                    chris.huston.t10 Level 3
                    Put a result handler in the pushtest HTTPService as in:
                    <mx:HTTPService id="pushtest" url=" http://www.mysite.com/pushtest.php" useProxy="false" method="POST" result="getit()"/>

                    I think the problem is that calling the gettest.send() function within the pushit function doesn't allow enough time for the MySQL to finish. The result handler works for me in a similar situation.

                    Vygo
                    • 7. Refreshing Datagrid?
                      Vision Airlines Level 1
                      I've already changed it to a result handler actually, but it still doesn't update. I believe the problem is related to the cache. the following works, but it's a rather odd workaround for something that should have a cleaner solution.

                      <?xml version="1.0" encoding="utf-8"?>
                      <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="vertical" creationComplete="getit()" viewSourceURL="srcview/index.html">
                      <mx:Script>
                      <![CDATA[

                      import mx.rpc.events.ResultEvent;
                      import mx.collections.ArrayCollection;

                      public function getit():void {
                      gettest.send()
                      }

                      public function pushit():void {
                      pushtest.send(addme)
                      textinput.text=""
                      gettest.url+="?"+String(Math.random()*999999) // This keeps gettest from pulling values from cache I think.
                      gettest.send()

                      }

                      [Bindable]
                      public var myData: ArrayCollection= new ArrayCollection();

                      public function handleGetResult(event:ResultEvent):void{
                      myData = event.result.users.user
                      }

                      ]]>
                      </mx:Script>
                      <mx:HTTPService id="gettest" url=" http://www.mysite.com/gettest.php" result="handleGetResult(event)"/>
                      <mx:HTTPService id="pushtest" url=" http://www.mysite.com/pushtest.php" useProxy="false" method="POST"/>

                      <mx:Model id="addme">
                      <myData>
                      <comment>{textinput.text}</comment>
                      </myData>
                      </mx:Model>

                      <mx:DataGrid id="datagrid" dataProvider="{myData}" width="100%" height="100%">
                      <mx:columns>
                      <mx:DataGridColumn headerText="id" dataField="id"/>
                      <mx:DataGridColumn headerText="comment" dataField="comment"/>
                      </mx:columns>
                      </mx:DataGrid>
                      <mx:HBox width="100%">
                      <mx:TextInput id="textinput" height="24" width="595"/>
                      <mx:Button label="Push" click="pushit()"/>
                      <mx:Button label="Get" click="getit()"/>
                      </mx:HBox>

                      </mx:Application>
                      • 8. Re: Refreshing Datagrid?
                        chris.huston.t10 Level 3
                        You have your result handler on your gettest HTTPService call.
                        If you put the result handler on your pushit HTTPService call you could avoid your workaround and the caching problems.

                        <mx:HTTPService id="gettest" url=" http://www.mysite.com/gettest.php"/>
                        <mx:HTTPService id="pushtest" url=" http://www.mysite.com/pushtest.php" useProxy="false" method="POST" result="getit()"/>

                        Vygo
                        • 9. Re: Refreshing Datagrid?
                          Vision Airlines Level 1
                          I really really wanted that to work. I did as you said and added the result="getit()" to the pushit service call and then I commented out the follwoing:

                          gettest.url+="?"+String(Math.random()*999999);

                          Still no refresh. Same caching issue.

                          Thanks for sticking with me and trying to help. Wish i knew why it's working for you.
                          • 10. Re: Refreshing Datagrid?
                            ntsiii Level 3
                            The caching problem is real, and your solution is one used by many.

                            If you have control over the server, you can set the headers to prevent caching. I dont have examples at hand, but you'll find them if you google.

                            Tracy
                            • 11. Refreshing Datagrid?
                              Vision Airlines Level 1
                              Thanks for the reply Tracy. I did the follow to solve this for now.

                              Sinse I have a php script that is generating the XML, I added this:

                              <?php
                              header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                              header('Cache-Control: no-store, no-cache, must-revalidate');
                              header('Cache-Control: post-check=0, pre-check=0', FALSE);
                              header('Pragma: no-cache');
                              ?>

                              This essentially guarantees that no Web browser or intervening proxy server will cache the page, so I will always receive the latest content. In fact, the first header should accomplish this on its own; this is the best way to ensure a page is not cached. The Cache-Control and Pragma headers are added for "insurance" purposes. Though they don't work on all browsers or proxies, they will catch some cases in which the Expires header doesn't work as intended (e.g. if the client computer's date is set incorrectly).

                              With the above solution, I was able to remove the following from my MXML:
                              gettest.url+="?"+String(Math.random()*999999); //This works, but not always