Jump to content

[SOLVED] I don't know why this getElementById... part won't work


Davidammit

Recommended Posts

 

<unimportant part>

I'm trying to make a site that embeds videos. All I want to have to update on said site is a database that will store the embed code. On my quest, I came across several languages that I believed would be of use to me. Javascript, PHP, XML and AJAX.

 

<important part>

I saw the example at http://www.w3schools.com/php/php_ajax_responsexml.asp where i thought that would be an excellent way to make it work. But when I tried to modify this code into something I thought would work for my site, it didn't work. I couldn't find out why. So I took the original code and put a bunch of alerts in the javascript portion, to find out that the

 document.getElementById("hometown").innerHTML=
xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;

part wasn't being executed. So I fiddled around with that for a little while. My main test was to change it to

 

 document.getElementById("hometown").innerHTML="IT WORKED!!";

 

and since that worked, I conluded that it must not have been able to find the XML tags or maybe the response wasn't generated.

 

If anyone can explain this or help me fix it I would greatly appreciate it.

 

PS: All I did to get the files was copy and paste the code into Notepad and save as .html, .js, and .php to satisfy the program, so I might need some sort of PHP or JavaScript compiling programs? However I did make the table they use under the user in the database jax_test in MySQL. And I created a user by the name peter with a password of abc123.

Link to comment
Share on other sites

xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;

 

getElementsByTagName requires tag names - 'div', 'p', 'ul', 'li' etc. You have given it an ID (or so it appears, unless by some chance you have created your own tags called 'hometown').

Link to comment
Share on other sites

In the example the PHP code is supposed to return XML.

 

The HTML:

 

<html>
<head>
<script src="responsexml.js"></script>
</head>
<body>

<form> 
Select a User:
<select name="users" onchange="showUser(this.value)">
<option value="1">Peter Griffin</option>
<option value="2">Lois Griffin</option>
<option value="3">Glenn Quagmire</option>
<option value="4">Joseph Swanson</option>
</select>
</form>

<h2><span id="firstname"></span>
 <span id="lastname"></span></h2>
<span id="job"></span>
<div style="text-align: right">
<span id="age_text"></span>
<span id="age"></span>
<span id="hometown_text"></span>
<span id="hometown"></span>
</div>

</body>
</html>

 

The JavaScript named responsexml.js:

 

var xmlHttp

function showUser(str)
{ 
xmlHttp=GetXmlHttpObject()
if (xmlHttp==null)
  {
  alert ("Browser does not support HTTP Request")
  return
  } 
var url="responsexml.php"
url=url+"?q="+str
url=url+"&sid="+Math.random()
xmlHttp.onreadystatechange=stateChanged 
xmlHttp.open("GET",url,true)
xmlHttp.send(null)
}

function stateChanged() 
{ 
if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")
{
xmlDoc=xmlHttp.responseXML;
document.getElementById("firstname").innerHTML=
xmlDoc.getElementsByTagName("firstname")[0].childNodes[0].nodeValue;
document.getElementById("lastname").innerHTML=
xmlDoc.getElementsByTagName("lastname")[0].childNodes[0].nodeValue;
document.getElementById("job").innerHTML=
xmlDoc.getElementsByTagName("job")[0].childNodes[0].nodeValue;
document.getElementById("age_text").innerHTML="Age: ";
document.getElementById("age").innerHTML=
xmlDoc.getElementsByTagName("age")[0].childNodes[0].nodeValue;
document.getElementById("hometown_text").innerHTML="<br/>From: ";
document.getElementById("hometown").innerHTML=
xmlDoc.getElementsByTagName("hometown")[0].childNodes[0].nodeValue;
}
} 

function GetXmlHttpObject()
{ 
var objXMLHttp=null
if (window.XMLHttpRequest)
  {
  objXMLHttp=new XMLHttpRequest()
  }
else if (window.ActiveXObject)
  {
  objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
  }
return objXMLHttp
}

 

And the PHP named responsexml.php:

EDIT: Whoah...Is that an error with ' I see? I copied this straight off the site.

EDIT: I recopied it and its still there even though there's a ' in the code when i post it

 

<?php
header('Content-Type: text/xml');
header("Cache-Control: no-cache, must-revalidate");
//A date in the past
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");

$q=$_GET["q"];

$con = mysql_connect('localhost', 'peter', 'abc123');
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("ajax_demo", $con);

$sql="SELECT * FROM user WHERE id = ".$q."";

$result = mysql_query($sql);

echo '<?xml version="1.0" encoding="ISO-8859-1"?>
<person>';
while($row = mysql_fetch_array($result))
{
echo "<firstname>" . $row['FirstName'] . "</firstname>";
echo "<lastname>" . $row['LastName'] . "</lastname>";
echo "<age>" . $row['Age'] . "</age>";
echo "<hometown>" . $row['Hometown'] . "</hometown>";
echo "<job>" . $row['Job'] . "</job>";
}
echo "</person>";

mysql_close($con);
?>

 

And the Table they use, which I made in MySQL under "ajax_demo" database with "user" for the tablename. I also made a user called "peter" with the password "abc123":

 

id  FirstName  LastName  Age  Hometown  Job

1 Peter Griffin 41 Quahog Brewery

2 Lois         Griffin 40 Newport Piano Teacher

3 Joseph Swanson 39 Quahog Police Officer

4 Glenn Quagmire 41 Quahog Pilot

 

 

Link to comment
Share on other sites

Ahh, the getElementsByTagName('hometown') wasn't wrong.

 

Your problem lies here:

 

echo <?xml version="1.0" encoding="ISO-8859-1"?>
<person>';

 

You can't echo out XML documents like that, and for a very good reason - the xml closing tag is the same as the php closing tag ( ?> ). So when the php gets to the end of the xml declaration, php ends. You can close the php tag before the xml declaration, output the xml declaration as is without using an echo statement, then reopen a php tag underneath it and start your echo statement again.

Link to comment
Share on other sites

  • 1 year later...

Ahh, the getElementsByTagName('hometown') wasn't wrong.

 

Your problem lies here:

 

echo <?xml version="1.0" encoding="ISO-8859-1"?>
<person>';

 

You can't echo out XML documents like that, and for a very good reason - the xml closing tag is the same as the php closing tag ( ?> ). So when the php gets to the end of the xml declaration, php ends. You can close the php tag before the xml declaration, output the xml declaration as is without using an echo statement, then reopen a php tag underneath it and start your echo statement again.

Can you give me an example of how it should be done then if you can't echo comments out like that? I did what the original poster has done - copied it straight from the W3Schools website but it won't work for me. It looks like it is the closing tag like you say but I don't understand how to get around that.

 

Thanks.

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.