IPB

Welcome Guest ( Log In | Register )

 
Reply to this topicStart new topic
> Php Aplication Help.
migroo
post Nov 2 2009, 05:39 AM
Post #1


Newbie
*

Group: Members
Posts: 28
Joined: 6-July 09
Member No.: 32,145



I can't understand why it is doing this to me!


Okay I have a xml document named "name.xml":
CODE
<?xml version="1.0" ?>
<person>
     <name>Laura Long</name>
     <age>34</age>
</person>


I have a page that is going to load and display the code:
CODE
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html>
<html>

<head>

<script type="text/javascript">
var xmlDoc;
if (window.XMLHttpRequest)
  {
  xhttp=new XMLHttpRequest();
  }
else // Internet Explorer 5/6
  {
  xhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xhttp.open("GET","name.xml",false);
xhttp.send("");
xmlDoc=xhttp.responseXML;

var x=xmlDoc.getElementsByTagName("person");

function show(i)
{
name=(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
age=(x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue);
txt="Name: "+name+"<br />Title: "+age+"<br />;
document.getElementById("show").innerHTML=txt;
}
</script>

</head>
<body>
<div id='show'>Names and age will be here</div>
<br />
<script type="text/javascript">
document.write("<table border='1'>");
for (var i=0;i<x.length;i++)
  {
  document.write("<tr onclick='show(" + i + ")'>");
  document.write("<td>");
  document.write(x[i].getElementsByTagName("name")[0].childNodes[0].nodeValue);
  document.write("</td><td>");
  document.write(x[i].getElementsByTagName("age")[0].childNodes[0].nodeValue);
  document.write("</td></tr>");
  }
document.write("</table>");
</script>
</body>
</html>


It just displays:
QUOTE
Names and age will be here

-

and nothing else!

I believe my problem is somewhere in the body part of the document but I am not sure I have bean messing around with if for 3 hours and am getting very angry with it.

So any help would be MUCH appreciated.

THANK YOU.

This post has been edited by migroo: Nov 2 2009, 05:42 AM
Go to the top of the page
 
+Quote Post
RahXephon
post Nov 2 2009, 05:51 AM
Post #2


Living under a rock.
***

Group: Members
Posts: 709
Joined: 24-January 06
From: 612 Wharf Avenue
Member No.: 2,173
Languages: XHTML, CSS, JavaScript, PHP



Edit: I've always seen HTML written with that. I guess not then. Last time I tried to create a <option> list with JavaScript and it was not displaying. But I'm pretty sure w3schools is spot on.

This post has been edited by RahXephon: Nov 2 2009, 06:30 AM
Go to the top of the page
 
+Quote Post
migroo
post Nov 2 2009, 06:05 AM
Post #3


Newbie
*

Group: Members
Posts: 28
Joined: 6-July 09
Member No.: 32,145



Okay I will give it a try. This is not my code I have just adjusted some code w3schools gives. Here is a like the where is am grabbing it.

The lesson:
http://w3schools.com/xml/xml_applications.asp

The "try it yourself":
http://w3schools.com/xml/tryit.asp?filename=tryxml_app



Thanks again for the input!


Go to the top of the page
 
+Quote Post
migroo
post Nov 2 2009, 04:03 PM
Post #4


Newbie
*

Group: Members
Posts: 28
Joined: 6-July 09
Member No.: 32,145



There example works just fine but when I try to use it with another xml file it just doesn't wan't to work and I have no idea why I can't see what I have done to it that would change it all that much.
I just changed the file that its supposed to open and changed the directories with so that it will read the new file.
I don't know if I just missed something or what (IMG:style_emoticons/default/umnik2.gif) .

Thank you for reading a posting.
Go to the top of the page
 
+Quote Post
justsomeguy
post Nov 2 2009, 04:07 PM
Post #5


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



Are you checking for Javascript errors?
Go to the top of the page
 
+Quote Post
migroo
post Nov 2 2009, 04:17 PM
Post #6


Newbie
*

Group: Members
Posts: 28
Joined: 6-July 09
Member No.: 32,145



I have no clue where the error is. So I am looking for any error I can find.

I am just writing this in notepad++ so I don't have any sort of checker.

Go to the top of the page
 
+Quote Post
justsomeguy
post Nov 2 2009, 04:24 PM
Post #7


The Old Man From Scene 24
*******

Group: Moderator
Posts: 14,826
Joined: 17-April 06
From: Phoenix
Member No.: 4,190
Languages: Focusing on PHP and JavaScript



Well, that's the problem. Every browser has some sort of error console that will display Javascript error messages. You can also download something like Firebug for Firefox and it will show error messages there as well. A major part of programming is responding to error messages, so you need to figure out where the error messages are going in your browser and make sure you look for them. They should tell you which line of code it detected an error on and what the error message was.
Go to the top of the page
 
+Quote Post
Rufus
post Nov 2 2009, 11:45 PM
Post #8


Newbie
*

Group: Members
Posts: 9
Joined: 13-October 09
From: Wisconsin
Member No.: 34,481
Languages: PHP, Javascript, HTML, CSS, XML



Since you posted this in the PHP forum maybe the PHP way would be useful to you. This example uses the SimpleXMLElement class built into PHP.

CODE
$xml = new SimpleXMLElement("person.xml", 0, true);


The SimpleXMLElement __construct method accepts five parameters, and I have used three in this example (really you only need 1). The first is data which is the xml data. You can either enter the xml data in here or a path to the file. If you use a path, you need the second two parameters. The second parameter is any options you wish to pass so I put a 0 here for "no options". Then the third parameter accepts true or false as to whether or not the data parameter is a url to file. So, I put true here to say that "person.xml" is a path to a file.

Whew! Ok now $xml is holding an object that holds the same data as your xml file. So to access the name node, use the following code (assuming you know how objects work). Keep in mind that the top tier node (<person>) becomes the top level of the object.

CODE
echo $xml->name;


Hope this helps if you choose not to use javascript at any point.

*edit

In fact now that I think about it, if I want to load xml using a javascript event such as onclick, I make an ajax request for the php file that returns the xml I need. Not sure if this is always the best way, but it works well for me in all cases so far.

This post has been edited by Rufus: Nov 2 2009, 11:49 PM
Go to the top of the page
 
+Quote Post
migroo
post Nov 3 2009, 04:50 AM
Post #9


Newbie
*

Group: Members
Posts: 28
Joined: 6-July 09
Member No.: 32,145



Thank you Rufus!
I like PHP much better than javascript. I never knew that there was a way to do this with PHP. So thank you again!

PS. sorry I posted this here I forgot I was under PHP.
Go to the top of the page
 
+Quote Post
Rufus
post Nov 4 2009, 01:27 AM
Post #10


Newbie
*

Group: Members
Posts: 9
Joined: 13-October 09
From: Wisconsin
Member No.: 34,481
Languages: PHP, Javascript, HTML, CSS, XML



Heh you are most welcome migroo!

Cheers
Go to the top of the page
 
+Quote Post

Reply to this topicStart new topic
1 User(s) are reading this topic (1 Guests and 0 Anonymous Users)
0 Members:

 



RSS Lo-Fi Version Time is now: 23rd November 2009 - 08:26 AM