Loading ...
Sorry, an error occurred while loading the content.

AutoComplete XML DataSource over XHR not working

Expand Messages
  • dvidluca
    Hi, I m just messing around with the AutoComplete control (no live site or anything). I ve got Apache and mySQL running on my home machine with PHP installed.
    Message 1 of 12 , Sep 12, 2007
    • 0 Attachment
      Hi,

      I'm just messing around with the AutoComplete control (no live site
      or anything). I've got Apache and mySQL running on my home machine
      with PHP installed. phpinfo() works, and I've created a basic
      database, can view it and pull results from it both with the mySQL
      command line and php so I know everything is set up right.

      I've got a php file which pulls the records from the database and
      outputs XML, I also know this works as I can veiw the XML output in
      my browser.

      Here's my autocomplete code:

      <script type="text/javascript" src="yui/yahoo-dom-event.js"></script>
      <script type="text/javascript" src="yui/connection-min.js"></script>
      <script type="text/javascript" src="yui/animation-min.js"></script>
      <script type="text/javascript" src="yui/autocomplete-min.js"></script>
      <script type="text/javascript">
      function init() {
      var dataSource = new YAHOO.widget.DS_XHR("getSuggetions.php",
      ["Result", "Data"]);
      dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
      var autoComp = new YAHOO.widget.AutoComplete
      ("autoInput", "resultsDiv", dataSource);
      }
      window.onload = init;
      </script>

      My XML is output like this:

      <ResultSet>
      <Result>
      <data>MyDataHere</data>
      </Result
      etc...
      </ResultSet>

      When I load the page with the AutoComplete control on it, the page
      loads fine, but when I type in the box, no suggestions from the
      DataSource are displayed. No javascript errors occur either when the
      page loads or when I type in the input field. I can even change the
      name of the php file to something random and non-existent and still
      no JavaScript errors appear so I guess that the file is not even
      being looked for. The php file is in the same folder as the html
      file that the control is on.

      Can anyone see what I'm doing wrong? I've made an AutoComplete using
      a JavaScript array and that worked perfectly...
    • dvidluca
      any pointers would be really appreciated, if you need to see my php code just ask, but like I said I think this is working ok as when I view the php file in my
      Message 2 of 12 , Sep 14, 2007
      • 0 Attachment
        any pointers would be really appreciated, if you need to see my php
        code just ask, but like I said I think this is working ok as when I
        view the php file in my browser I se the XML output as expected...
      • dvidluca
        ok, my server setup is perfect, I can use traditional AJAX techniques and the XML is returned ok so it must be my YUI code I installed firebug and the DOM tab
        Message 3 of 12 , Sep 14, 2007
        • 0 Attachment
          ok, my server setup is perfect, I can use traditional AJAX techniques
          and the XML is returned ok so it must be my YUI code

          I installed firebug and the DOM tab says "XHR response failed" under
          DS_XHR and below this it says

          TYPE_FLAT 2
          TYPE_XML 1

          Below this under DataSource it says
          "Response data was null"
          "Response data could not be parsed"

          Sorry, I know its a pain without a live page to look at...

          Could the above debug mean that my schema is wrong? And how come Type
          flat is metnioned when I am not using this?
        • dvidluca
          oh, also do you need to use an XSD schema document with the DS_XHR datasource?
          Message 4 of 12 , Sep 14, 2007
          • 0 Attachment
            oh, also do you need to use an XSD schema document with the DS_XHR
            datasource?
          • jennykhan
            Hi there, Try changing your DataSource constructor from: var dataSource = new YAHOO.widget.DS_XHR( getSuggetions.php , [ Result , Data ]); to: var dataSource
            Message 5 of 12 , Sep 14, 2007
            • 0 Attachment
              Hi there,

              Try changing your DataSource constructor from:

              var dataSource = new YAHOO.widget.DS_XHR("getSuggetions.php",
              ["Result", "Data"]);

              to:

              var dataSource = new YAHOO.widget.DS_XHR("getSuggetions.php",
              ["Result", "data"]);

              Regards,
              Jenny



              --- In ydn-javascript@yahoogroups.com, "dvidluca" <dvidluca@...> wrote:
              >
              > Hi,
              >
              > I'm just messing around with the AutoComplete control (no live site
              > or anything). I've got Apache and mySQL running on my home machine
              > with PHP installed. phpinfo() works, and I've created a basic
              > database, can view it and pull results from it both with the mySQL
              > command line and php so I know everything is set up right.
              >
              > I've got a php file which pulls the records from the database and
              > outputs XML, I also know this works as I can veiw the XML output in
              > my browser.
              >
              > Here's my autocomplete code:
              >
              > <script type="text/javascript" src="yui/yahoo-dom-event.js"></script>
              > <script type="text/javascript" src="yui/connection-min.js"></script>
              > <script type="text/javascript" src="yui/animation-min.js"></script>
              > <script type="text/javascript" src="yui/autocomplete-min.js"></script>
              > <script type="text/javascript">
              > function init() {
              > var dataSource = new YAHOO.widget.DS_XHR("getSuggetions.php",
              > ["Result", "Data"]);
              > dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_XML;
              > var autoComp = new YAHOO.widget.AutoComplete
              > ("autoInput", "resultsDiv", dataSource);
              > }
              > window.onload = init;
              > </script>
              >
              > My XML is output like this:
              >
              > <ResultSet>
              > <Result>
              > <data>MyDataHere</data>
              > </Result
              > etc...
              > </ResultSet>
              >
              > When I load the page with the AutoComplete control on it, the page
              > loads fine, but when I type in the box, no suggestions from the
              > DataSource are displayed. No javascript errors occur either when the
              > page loads or when I type in the input field. I can even change the
              > name of the php file to something random and non-existent and still
              > no JavaScript errors appear so I guess that the file is not even
              > being looked for. The php file is in the same folder as the html
              > file that the control is on.
              >
              > Can anyone see what I'm doing wrong? I've made an AutoComplete using
              > a JavaScript array and that worked perfectly...
              >
            • dvidluca
              thanks, I tried this but it didnt work. I changed the PHP so the XML is output all in lowercase and changed the JS schema to [ result , data ] but this
              Message 6 of 12 , Sep 14, 2007
              • 0 Attachment
                thanks, I tried this but it didnt work. I changed the PHP so the XML
                is output all in lowercase and changed the JS schema to
                ["result", "data"] but this didn't help either...

                Firebug is pointing towards a parsing error so maybe the PHP is
                wrong? Here is the PHP code:

                <?php

                $host = "localhost";
                $user = "root";
                $password = "password";
                $database = "mydata";
                $server = mysql_connect($host, $user, $password) or die("Connection
                Problem");
                $connection = mysql_select_db($database, $server) or die("Database
                Problem");
                $query = mysql_query("SELECT * FROM suggestions ORDER BY
                suggestion") or die("Table Problem");


                $xmloutput = "<?xml version=\"1.0\"?>\n";
                $xmloutput .= "<resultset
                xmlns:xs=\"http://www.w3.org/2001/XMLSchema\"
                xs:schemaLocation=\"mySchema.xsd\">\n";

                for ($x = 0; $x < mysql_num_rows($query); $x++) {
                $row = mysql_fetch_assoc($query);
                $xmloutput .= "\t<result>\n";
                $xmloutput .= "\t\t<data>" . $row['sugestion'] . "</data>\n";
                $xmloutput .= "\t</result>\n";
                }

                $xmloutput .= "</resultset>";

                echo $xmloutput;

                mysql_close($server);
                ?>
              • dvidluca
                Ok, I totally changed my javascript to use TYPE_FLAT instead and changed the PHP file to output plain text. There is only one field and the record delimeter
                Message 7 of 12 , Sep 15, 2007
                • 0 Attachment
                  Ok, I totally changed my javascript to use TYPE_FLAT instead and
                  changed the PHP file to output plain text. There is only one field and
                  the record delimeter is a newline \n

                  javascript is now:

                  var dataSource = new YAHOO.widget.DS_XHR("getData.php", ["\n"]);
                  dataSource.responseType = YAHOO.widget.DS_XHR.TYPE_FLAT;

                  FireBug still shows *exactly* the same information; it still has a 1
                  next to TYPE_XML even though I'm not even using it now and the errors
                  are the same. i can see the plain text perfectly in my browser.

                  What is going on?!
                • dvidluca
                  I got the TYPE_FLAT sort of working now. The problem is, all of the items from my database are placed in the suggestion div each time the autocomplete is typed
                  Message 8 of 12 , Sep 15, 2007
                  • 0 Attachment
                    I got the TYPE_FLAT sort of working now.

                    The problem is, all of the items from my database are placed in the
                    suggestion div each time the autocomplete is typed in. So if I type a
                    b into the box, instead of just getting the words that start with b, I
                    get all of the items from the database.

                    Surely this isn't right?
                  • dvidluca
                    ok, got TYPE_FLAT working perfectly, I didn t code for the query string properly but I fixed it. The TYPE_XML is almost working now as well, I get the XML
                    Message 9 of 12 , Sep 16, 2007
                    • 0 Attachment
                      ok, got TYPE_FLAT working perfectly, I didn't code for the query string
                      properly but I fixed it.

                      The TYPE_XML is almost working now as well, I get the XML response from
                      the PHP file but the suggestion div is not displayed...any ideas?
                    • hugohiram
                      Hello, Did you fixed it? I am having similar problems, it s working on TYPE_FLAT, but cannot make it work on TYPE_XML. regards, Hugo.
                      Message 10 of 12 , Sep 25, 2007
                      • 0 Attachment
                        Hello,

                        Did you fixed it? I am having similar problems, it's working on
                        TYPE_FLAT, but cannot make it work on TYPE_XML.

                        regards,
                        Hugo.
                        ---

                        --- In ydn-javascript@yahoogroups.com, "dvidluca" <dvidluca@...> wrote:
                        >
                        > ok, got TYPE_FLAT working perfectly, I didn't code for the query string
                        > properly but I fixed it.
                        >
                        > The TYPE_XML is almost working now as well, I get the XML response from
                        > the PHP file but the suggestion div is not displayed...any ideas?
                        >
                      • David Lucas
                        No, I kind of scratched around for a solution for a few days and then just went with TYPE_FLAT I d like to get it working if I can though.... ... Yahoo!
                        Message 11 of 12 , Sep 25, 2007
                        • 0 Attachment
                          No, I kind of scratched around for a solution for a few days and then just went with TYPE_FLAT
                           
                          I'd like to get it working if I can though....


                          Yahoo! Answers - Get better answers from someone who knows. Try it now.
                        • Richard Biddle
                          Hi David, I also had the same problem with an xml datasource. The problem is that the MIME type of the returned results must be text/xml This can be set in
                          Message 12 of 12 , Oct 1, 2007
                          • 0 Attachment
                            Hi David,
                            I also had the same problem with an xml datasource.
                            The problem is that the MIME type of the returned results must be
                            'text/xml'
                            This can be set in the first line of your php file with:
                            <?php header("Content-Type: text/xml"); ?>
                            and it all works.
                            Hope it works for you too.
                            Richard

                            --- In ydn-javascript@yahoogroups.com, David Lucas <dvidluca@...> wrote:
                            >
                            > No, I kind of scratched around for a solution for a few days and
                            then just went with TYPE_FLAT
                            >
                            > I'd like to get it working if I can though....
                            >
                            >
                            > ---------------------------------
                            > Yahoo! Answers - Get better answers from someone who knows. Tryit now.
                            >
                          Your message has been successfully submitted and would be delivered to recipients shortly.