Jump to content

importing data from an XML to a php-page without reloading


pake

Recommended Posts

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...  :D 

 

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]

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

I found a better(?) way to get what I want. Thank God for jquery  :P

 

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.  :D

 

 

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');"> ? :confused:

Link to comment
Share on other sites

I found a better(?) way to get what I want. Thank God for jquery  :P

 

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.  :D

 

 

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');"> ? :confused:

 

I prefer using the second...just two seperate ways of doing it

Link to comment
Share on other sites

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.