Jump to content

Recommended Posts

I must be missing something simple here. Ive setup a few simple files for testing, but I cant for the life of me get any output produced.

 

front.php

<html>
    <head>
        <title>ajax test</title>
        <script type="text/javascript" language="javascript" src="/media/js/jquery/jquery.js"></script>
        <script type="text/javascript">
            $(document).ready(function() {
                $.get('http://dev.oblivion/back.php', {}, function() {
                    $(this).find('users').each(function() {
                        $(this).find('user').each(function() {
                            var fname = $(this).find('fname').text();
                            var lname = $(this).find('lname').text();
                        });
                    });
                });
                $('#foo').html(fname + ' ' + lname);
            });
        </script>
    </head>
    <body>
        <div id="foo"></div>
    </body>
</html>

 

back.php

<?php

$xml = "<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>\n";
$xml .= "<users>\n";
$xml .= " <user>\n";
$xml .= "  <fname>thorpe</fname>\n";
$xml .= "  <lname>hepburn</lname>\n";
$xml .= " </user>\n";
$xml .= "</users>";

header("Content-Type: text/xml");
echo $xml;

?>

 

Any pointers much appreciated.

Link to comment
https://forums.phpfreaks.com/topic/158899-solved-jquery-parse-xml/
Share on other sites

this might help a bit it's a lower level implementation of the ajax functionality. the variables fname and lname are out of the scope when you try to append it to a div.

$(document).ready(function(){
$.ajax({
	type: "POST",
	url: "xml.php",
	dataType: "xml",
	success: function(xml) {
		$(xml).find('users').each(function(){
			var lname= $(this).find('lname').text();
			var fname = $(this).find('fname').text();
			console.log(lname);
			console.log(fname);


		});
	}
});
//out of the scope will return "undefined undefined"
$('#foo').html(fname + ' ' + lname);
});

Thanks DJ. I feel like a bit of a dill. You know? I was looking at the function calls as simple loops, console.log pointed out that I was actually getting the data (with a few adjustments), so with that in place it was easy enough to spot that lname fname where out of scope by the time I tried adding them to the #foo div.

 

All fixed now, I did have to initialise data to be an empty object however because I was getting a 411 error.

 

Anyway, thanks heaps. Its not very often I do much client side stuff. I'm loving jQuery though.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.