views:

169

answers:

3

I have a small script, where on change I am getting the ID of a select, I then want to take that ID and send it via "data:" through the jQuery AJAX call. I am using XML for my data and I am sending this ID for it only returns results with that specific ID?

I understand how to do this with PHP, but never worked with XML and jQuery before.

Thoughts?

Thanks,

Ryan

UPDATE:

Here is a piece of my code, sorry I didnt post it before.

function populateDropDown(){
    console.log("populateDropDown is called");
    $(".user-selection").show();
    $.ajax({
     type: "GET",
     url: "employee.xml",
     dataType: "xml",
     data: "access_info.level"+ type_id,
     success: function(xml) {
      console.log("success!");
      var select = $('#select-user');
      $(xml).find('employee').each(function(){
       var fn = $(this).find('first_name').text();
       var ln = $(this).find('last_name').text();
       //var value = $(this).find('access_level').text();
       select.append("<option class='ddindent' value='"+ fn +"'>"+fn +" "+ ln +"</option>");
      });
      select.children(":first").text("Please make a selection").attr("selected",true);
     }
    });
    console.log("from popdrop"+type_id)
}

That is an exmaple, I need to send a parameter to only return X results, in this case its an ID that I grabbed from the drop-down.

+2  A: 

Using the AJAX call in jQuery you can accomplish something like this:

$('#yourselect').change(function(){ 
$.ajax({
 type: "GET",
 url: "employee.xml",
 dataType: "xml",
 data: "id=" + $(this).val,
 success: function(xml){
    //code to handle the successful return from the server
    //your existing handler should work here
  }
 });
});

However, it may be easier to use one of the higher level functions as found in the forms plugin for submitting a form via AJAX.

UPDATE: OK. Now I see your update. I think this may still help.

Vincent Ramdhanie
will this allow XML as well? say I need <access_info level="2"> the level = 2, part. I want to return all results with level of 2. the parent of access_info is employee
Coughlin
I am going to run this call, then print the results of the XML on the HTML page. But I want to see only specific data..thank you Vincent
Coughlin
I modified slightly to show how your code will work with this code. Hope that helps.
Vincent Ramdhanie
would my data look like data:access_info.level = XXX
Coughlin
Thats right. I used id in the example because I did not know your parameter name.
Vincent Ramdhanie
I see! I have it not erroring anymore, my XMl is here: http://pastie.org/744436 and also it doesnt seem to be returning just those results. still printing all of them. my original questions has updated code. thanks again! appreciate it.
Coughlin
I notice that the URL you are using is employee.xml. Does that mean that you are actually loading an xml file? You probably need some server side code like php that will accept the parameter that you are sending and construct the xml document for you dynamically then return only those parts that are relevant.
Vincent Ramdhanie
A: 

If you are interested in parsing the AJAX response, here is a quick and dirty XML parser example block for jQuery:

  $(request.responseXML).find("marker").each(function() {
      var marker = $(this);
      var point = {
          marker.attr("lat"),
          marker.attr("lng")
      };
  });
r00fus
A: 

Here is some code i had for working with Google maps, Jquery and XML. Should show you how to get a file and parse it, as well as execute any new methods.

$(".map-overlay-left").click(function () {  
    var map = new GMap2(document.getElementById('map-holder'));
    $("#map-holder").hide('slow');         
    var gmarkers = [];
    var side_bar_html = "";
    map.addControl(new GSmallMapControl());
    map.addControl(new GMapTypeControl());
    var Africa = new GLatLng(-2.767478,23.730469);
    map.setCenter(Africa, 4); 

    $.get("http://xx.xxx.xxxx.xxx/xml-feed-google-maps",{},function(xml) {
     $('marker',xml).each(function(i) {
      html = $(this).text();
      lat = $(this).attr("lat");
      lng = $(this).attr("lng");
      label = $(this).attr("label");
      var point = new GLatLng(lat,lng);
      var marker = createMarker(point,label,html);
      map.addOverlay(marker);
     });
    });

    $("#map-holder").show('slow');
});

The XML File looks like this:

<markers> 
    <marker lat="11.547812" lng="104.915957" label="foo"> 
     <infowindow><![CDATA[><div class="infobox"><h2><a href="link">Anchor Text</a></h2><p>HTML Content</p></div>]]></infowindow> 
    </marker> 
    <marker lat="11.547812" lng="104.915957" label="foo"> 
     <infowindow><![CDATA[><div class="infobox"><h2><a href="link">Anchor Text</a></h2><p>HTML Content</p></div>]]></infowindow> 
    </marker> 
    <marker lat="11.547812" lng="104.915957" label="foo"> 
     <infowindow><![CDATA[><div class="infobox"><h2><a href="link">Anchor Text</a></h2><p>HTML Content</p></div>]]></infowindow> 
    </marker> 
<markers>
Shadi Almosri