0

I have a php file (myxml.php) that uses a 3rd-party API to return XML. It echoes the XML on the last line.

 $xml = file_get_contents($url);
 echo $xml;

Another php file (index.php) uses jQuery to read this XML in:

$(document).ready(function(){
        $.ajax({
            type: "GET",
            url: "myxml.php",
            dataType: "xml",
            success: function(xml) {

My index.php file needs to pass the myxml.php file a variable called 'searchitem' that it can use before calling the API.

How do I pass $searchitem to myxml.php ?

Also, for better performance, can I import the php variable $xml to my ajax code above without the need to echo it?

Thanks in advance

3
  • how are you calling myxml.php - from a browser ? Commented Nov 14, 2012 at 14:26
  • What have you done until now, show us some code :) Commented Nov 14, 2012 at 14:27
  • Code added to explain, thanks guys. Commented Nov 14, 2012 at 14:51

1 Answer 1

4

You apparently are using jQuery.

Just add it to the url (so build the jQuery syntax with php)

    $.ajax({
        type: "GET",
        url: "myxml.php?searchitem=<?php echo $searchitem; ?>",
        dataType: "xml",
        success: function(xml) {

In turn, on myxml.php you can get this with the $_GET variable.

$searchitem = $_GET['searchitem'];


Without echo? Only possible if you don't need ajax, so you can just include the myxml.php file into the index.php file. But I assume there is a good reason for doing ajax

Sign up to request clarification or add additional context in comments.

5 Comments

Why not just set a variable and include the file within PHP?
because he wants to use ajax
my answer still applies, but I'll change it a bit to fit your additions
Rene, that's working now, thank you very much. I just need to add in some code to show that it's doing something while off fetching the XML results. I'm only using Ajax for ease of use reading the XML, would php parsing of the XML be significantly quicker?
it would happen on load, so the user has to wait for the xml to be downloaded from the 3rd party. If you cache it locally it would make sense to do it with php. But that is a whole different question. Doesn't relate to this one. So if you want to go that way, try it first and if it doesn't work, open a question for that. For the time being, you could accept this answer since it helped you right?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.