tags:

views:

34

answers:

2

Hi I am new to Jquery and struggling with parsing XML. I have a search box in my form that returns the results of a sql search when the user has entered the first few letters . this string is then searched with a php file and the results are returned in the form of an xml file. The problem is that I need to uniquely identify each name and id returned and then to be able to access a specific name/id set. so that this one item is shown on my form. The code (I thought) would return a list of all the the names but this seems to be in just one string. Can anybody please help me with accessing individual xml elements . I have included the xml file at the bottom , thanks for any help

        // JQ - ajax  
         $(document).ready(function(){ 
             $.ajax({
                      type: "GET", 
                      url: "search_action.php?" + string ,
                      dataType: "xml",
                      success: disxml ,

                 });


        })
        }

         function disxml(data){

        $(data).find('results').each(function() {  

        var name      = $(this).find('name').text();   
        var cus_id    = $(this).find('id').text(); 


        })  

The XML File

      <results>

      <name>
      The porter grp
      </name>
      <id>
      548930
      </id>

      <name>
      ADI UK
      </name>
      <id>
      986565
      </id>

      <name>
      Fireled
      </name>
      <id>
      549899
      </id>

      <name>
      JCK Rec LTD
      </name>
      <id>
      548442
      </id>

      </results> 
+1  A: 

Have you ever heard of json?

Javascript can handle json beautifully. So my suggestion is that you serialize your data to json instead of XML and feed this to javascript.

Looky here for the how.

Peter
+3  A: 

It may help to change the XML schema to include a result node containing the name and id elements.

<results>
  <result>
      <name>
      The porter grp
      </name>
      <id>
      548930
      </id>
  </result>
  <result>
      <name>
      ADI UK
      </name>
      <id>
      986565
      </id>
  </result>

</results> 

Your jQuery code should then do something like

function disxml(data){

        $(data).find('result').each(function() {  

        var name      = $(this).find('name').text();   
        var cus_id    = $(this).find('id').text(); 


        })  

Regards

Howard May