I am creating an application with JQuery as the UI. The current, what I have now is using simple xml as the Select Menu options.

What I am wanting to do is, when the user selects the Hub they want, use simple xml to get the data for that hub.

The xml file I am getting from is

They do have Hub Id's but I am unsure of how to distribute out the data when the user chooses a hub.

I am using this.... for Simple_Xml

<?php if($xml === false)
	{
	   echo "There was an error opening the xml file.";
		 exit;
		   }
		else
		 {
		foreach($xml as $hubname)
		 {
		echo "<option value='option_Hubname'>" . $hubname->name . "</option>";
		 }
	}
?>

That is for the options. But for the data for each hub I am guessing I will have to use a different simple xml method?

I need some help.. .. .. Thanks in advance.

Dani AI

Generated

Short version: put each hub ID in the <option value="...">, then request the chosen hub’s data from the server when the select changes. That keeps the UI snappy and avoids embedding all details in the page. As suggested, the value must be the id; as suggested, an AJAX/XHR approach is the usual way to do an “auto postback.”

A simple, robust flow:

  • Populate the <select> with value=hub_id (server-side when generating the options).
  • Bind a change handler in jQuery that sends the selected id to a PHP endpoint.
  • In PHP use SimpleXML (or xpath) to find the matching hub node, validate/sanitize the id, then return a small JSON payload. Update the page from the JSON.

Example client (jQuery):

$(function(){
  $('#hubSelect').on('change', function(){
    var id = $(this).val();
    if(!id) return;
    $.getJSON('getHub.php', { id: id })
      .done(function(data){
        if(data.error) { console.warn(data.error); return; }
        $('#hubName').text(data.name);
        $('#hubCity').text(data.city);
        // update other fields
      })
      .fail(function(xhr, stat, err){ console.error('AJAX error', stat, err); });
  });
});

Example server logic (PHP, using SimpleXML + JSON output):

<?php
header('Content-Type: application/json; charset=utf-8');
$id = isset($_GET['id']) ? intval($_GET['id']) : 0;
// load XML (local file or server-side fetch)
$xml = simplexml_load_file('path/to/hubs.xml');
$match = $xml->xpath("//hub[id={$id}]");
if(!$match) { echo json_encode(['error'=>'not found']); exit; }
$hub = $match[0];
echo json_encode(['id'=>(string)$hub->id,'name'=>(string)$hub->name,'city'=>(string)$hub->city]);

Troubleshooting / cautions:

  • Cross-domain: browsers block XHR to other domains unless CORS is enabled or you proxy the request server-side.
  • Sanitize id on the server (cast to int) to avoid injection.
  • If the XML is small, consider loading it once client-side (dataType: "xml") and filtering in JS to avoid extra calls.
  • Check the browser console and network tab for the request URL and response if things don’t appear.

Recommended Answers

All 5 Replies

I'm not really sure what's in $xml, but you'll need to put something in the value attribute, some id or that name. Then get the data that matches that.

hey Dude. I saw the leakage here right now.... you cannot just set XML on just a server side script. u need also the client side script. Twiss was Right. I have already coded the right AUTO POST BACK function of PHP using XML, HTML and AJAX. You Have to Code HTML and inside HTML there was an AJAX or a JavaScript code then inside of that function... you'll have to construct a code of XML. the key here is this what you have to do... XMLHttpRequest() <---( this XML special function is where you will request your own php file without any button click events )... to know more about this... try to read and understand it your self in searching online. :)

That's true, you could use ajax, but you don't have to. A normal form would in this case be appropriate, I think.

That's true, you could use ajax, but you don't have to. A normal form would in this case be appropriate, I think.

Sometimes it is done with AJAX or JavaScript or JQuery. even if it is in a normal form but it seems to be he want's an auto post back similar to ASP.net :)


Anyway twiss can you help me out in this... I posted it here in Daniweb PHP forum

http://www.daniweb.com/web-development/php/threads/364934

Ehm, that's a link to this thread?

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.