pake Posted May 18, 2011 Share Posted May 18, 2011 Hi guys. Here's my story. I've done a few html and flash sites before but now I wanted to learn php and AJAX too. I don't consider myself as a real programmer (even though I use perl daily at work). To be honest, I'm more of a "copy-paste-edit"-kind of guy who has way too little time. I'd like to get help in order to get things going on my quest to learn php & AJAX... I'm creating a new website for our floorball team and I'd like to use XMLs to import player data to each players own page. (I chose XML since it would be easier to update the data/statistics). Instead of making a php-file/page (+an xml-file) for each individual player, I'd like to have only one php-page that gets all the data it needs from xml-files (player1.xml, player2.xml etc.) without reloading the whole page. In my vision there's a list of players (as a menu) and when you click on a name a script loads the chosen player's xml and passes on the data (picture+statistics) to the content section of the page. I guess I should use AJAX for that, am I right? I've tried googleing but haven't really found a usable tutorial / example yet. Anyone wanna help me out here? I was thinking of using XMLs like this: <?xml version="1.0" encoding="utf-8"?> <player name="Eric Example"> <picture url="images/example.jpg"> <stats value="2009-2010"> <games>17</games> <goals>27</goals> <assists>20</assists> <points>47</points> <penalties>2</penalties> </stats> <stats value="2010-2011"> <games>10</games> <goals>15</goals> <assists>15</assists> <points>30</points> <penalties>5</penalties> </stats> </player> I would like to get the stats from the xml to be shown in a table form (see the attachment) so that it automatically adds the necessary rows when the data grows. Am I way over my head here and if so, is anyone willing to help me out? Or should I do things in some other (better) way? [attachment deleted by admin] Quote Link to comment Share on other sites More sharing options...
fugix Posted May 18, 2011 Share Posted May 18, 2011 check this out https://developer.mozilla.org/en/AJAX/Getting_Started step 4 explains how to grab data from an xml file using an xml response header Quote Link to comment Share on other sites More sharing options...
pake Posted May 20, 2011 Author Share Posted May 20, 2011 Thanks. I tried to modify that example to suit my needs but didn't succeed. Here's my problem: I have the javascript (makeRequest-function etc) in the <head>-tag of the php-file. I created a variable "testi" which contains the xml's content. However, I can't use that variable later in the php-file (in the <body>-tag), like this: <body> <div id="content"> . . <div id="playerPicture"> <script type="text/javascript"> document.write(testi); </script> </div> . . </div> . . </body> How should I fix this problem? Should I put that <script>-tag inside the <div id="content">-tag or is there a way to get that variable work outside the <head>-tag? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 23, 2011 Share Posted May 23, 2011 your div syntax is correct..but why cant you simply move your ajax to the body tag Quote Link to comment Share on other sites More sharing options...
pake Posted May 23, 2011 Author Share Posted May 23, 2011 I found a better(?) way to get what I want. Thank God for jquery In team.php's head-tag there's: <script type="text/javascript" src="includes/jquery-1.6.1.min.js"></script> <script type="text/javascript" language="javascript"> function loadContent(elementSelector, sourceURL) { $(""+elementSelector+"").load(""+sourceURL+""); } </script> and in the player selection menu there's: <a href="#" onclick="loadContent('#content', 'team_players.php?id=player1.xml')"> where the xml filename varies. The whole content of the page is in the team_players.php where I'm supposed to import the xml's data into tables etc. After some initial testing I'm pretty sure I will be asking for help in a few days on the "import data from xml to tables"-issue... I'll try to figure it out myself first and if I can't manage --> I'll be back. BTW. Which is better (and why?) 1) <a href="#" onclick="loadContent('#content', 'team_players.php?id=player1.xml')"> or 2) <a href="javascript:loadContent('#content', 'team_players.php?id=player1.xml');"> ? Quote Link to comment Share on other sites More sharing options...
fugix Posted May 23, 2011 Share Posted May 23, 2011 I found a better(?) way to get what I want. Thank God for jquery In team.php's head-tag there's: <script type="text/javascript" src="includes/jquery-1.6.1.min.js"></script> <script type="text/javascript" language="javascript"> function loadContent(elementSelector, sourceURL) { $(""+elementSelector+"").load(""+sourceURL+""); } </script> and in the player selection menu there's: <a href="#" onclick="loadContent('#content', 'team_players.php?id=player1.xml')"> where the xml filename varies. The whole content of the page is in the team_players.php where I'm supposed to import the xml's data into tables etc. After some initial testing I'm pretty sure I will be asking for help in a few days on the "import data from xml to tables"-issue... I'll try to figure it out myself first and if I can't manage --> I'll be back. BTW. Which is better (and why?) 1) <a href="#" onclick="loadContent('#content', 'team_players.php?id=player1.xml')"> or 2) <a href="javascript:loadContent('#content', 'team_players.php?id=player1.xml');"> ? I prefer using the second...just two seperate ways of doing it Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.