Jump to content

Tutorial - missing something maybe?


DavidPr

Recommended Posts

i had the smae thing happen 2,when you copy paste it it seems like it gets pasted as one big line,go throught it and break the lines is a more resonable look, spacve out the {} to make reasier to read,aaah, what the heckhere it is....

var xmlHttp;function init_ajax(){  try  {	xmlHttp=new XMLHttpRequest();  }  catch (e)  {	try	{	  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");	}	catch (e)	{	  try	  {		xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");	  }	  catch (e)	  {		alert("Your browser does not support AJAX!");	  }	}  }}}

This one works, cause i use it.

Link to comment
Share on other sites

Here's the JavaScript code "getrss.js" that is in the tutorial. I'm also using the AJAX database tutorial and it works fine, but this one isn't.

var xmlHttpfunction showRSS(str) { xmlHttp=GetXmlHttpObject()if (xmlHttp==null) { alert ("Browser does not support HTTP Request") return } var url="getrss.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")  {   document.getElementById("rssOutput").innerHTML=xmlHttp.responseText   } }function GetXmlHttpObject(){var xmlHttp=null;try { // Firefox, Opera 8.0+, Safari xmlHttp=new XMLHttpRequest(); }catch (e) { // Internet Explorer try  {  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");  } catch (e)  {  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");  } }return xmlHttp;}

Link to comment
Share on other sites

I can't tell if this is caused by the forum or what, but this:
  document.getElementById("rssOutput").innerHTML=xmlHttp.responseText

should all be on one line.

Looks like it is caused by the forum... :)http://www.w3schools.com/php/getrss.jsI don't understand why though. The text shouldn't break in the code-window, I don't think it's too wide. Perhaps it's some security feature..?
Link to comment
Share on other sites

The script that I'm using has it all on one line.

document.getElementById("rssOutput").innerHTML=xmlHttp.responseText

Here's "getrss.php"

<?php//get the q parameter from URL$q=$_GET["q"];//find out which feed was selectedif($q=="Google") { $xml=("http://news.google.com/news?ned=us&topic=h&output=rss"); }elseif($q=="MSNBC") { $xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"); }elseif($q=="WSFA") { $xml=("http://www.wsfa.com/Global/category.asp?C=62542&clienttype=rss"); }elseif($q=="FOX") { $xml=("http://www.foxnews.com/xmlfeed/rss/0,4313,0,00.rss"); }$xmlDoc = new DOMDocument();$xmlDoc->load($xml);//get elements from "<channel>"$channel=$xmlDoc->getElementsByTagName('channel')->item(0);$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;//output elements from "<channel>"echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");echo("<br />");echo($channel_desc . "</p>");//get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>"); echo ("<br />"); echo ($item_desc . "</p>"); }?>

The html form:

<form> Select a News Sorce:<select name="rss" onchange="showRSS(this.value)"><option>Select One</option><option value="FOX">FOX News</option><option value="WSFA">WSFA Local News</option><option value="Google">Google News</option><option value="MSNBC">MSNBC News</option></select></form><p><div id="rssOutput">News will display here.</div></p>

And this in the head tags:

<script type='text/javascript' src='getrss.js'></script>

Link to comment
Share on other sites

What is getrss.php?
The server page called by the JavaScript code is a PHP file called "getrss.php":The PHP Page ExplainedWhen an option is sent from the JavaScript the following happens:* PHP finds out which RSS feed was selected.* An XML DOM object is created for the selected RSS feed.* The elements from the RSS channel are found and outputted.* The three first elements from the RSS items are looped through and outputted.**********************I don't get an error message displayed on the screen just this at the bottom of the page just above the Window's start button: Yellow triangle with exclamation mark and "error on page" message. (see attachment)Nothing shows up where this is:
<p><div id="rssOutput"><b>RSS Feed will be listed here.</b></div></p>

post-16885-1189463919_thumb.jpg

Link to comment
Share on other sites

I wonder which one is undefined. Change the callback function to this:

function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")  { 	alert(document.getElementById("rssOutput"));	alert(xmlHttp.responseText);	//document.getElementById("rssOutput").innerHTML=xmlHttp.responseText;  } }

You should see two message boxes pop up, do either of them say something like undefined or null?

Link to comment
Share on other sites

It would only do that if the rssOutput element doesn't exist on the page. Look at this code:

<html>  <head>	<title>JS Test</title>	<script type="text/javascript">	function test()	{	  alert("rssOutput: " + typeof(document.getElementById("rssOutput")));	  alert("xmlHttp: " + typeof(xmlHttp));	  alert("rssOutput.innerHTML: " + typeof(document.getElementById("rssOutput").innerHTML));	}	</script>  </head>  <body onload="test();">	 	<div id="rssOutput"></div>  </body></html>

If the div with the id rssOutput exists, you get 3 alert boxes saying what the types of each thing are. If you remove that div, the first box still says object, the second box still says undefined (which it should), and the third box causes the error that you are seeing, because it tries to access a property of a nonexistent object. So you need to figure out why the Javascript can't find the div on the page. If you want us to look at it, post the entire code instead of just part of it.

Link to comment
Share on other sites

Here's the result of the test using the script you posted:rssOutput: objectxmlHttp: undefinedrssOutput.innerHTML: stringHere's the getrss.js I'm using:

var xmlHttpfunction showRSS(str){xmlHttp=GetXmlHttpObject()if (xmlHttp==null){alert ("Browser does not support HTTP Request")return}var url="includes/getrss.php"url=url+"?q="+strurl=url+"&sid="+Math.random()xmlHttp.onreadystatechange=stateChangedxmlHttp.open("GET",url,true)xmlHttp.send(null)}function stateChanged() { if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete")  { 	alert(document.getElementById("rssOutput"));	alert(xmlHttp.responseText);	//document.getElementById("rssOutput").innerHTML=xmlHttp.responseText;  } }function GetXmlHttpObject(){var xmlHttp=null;try{// Firefox, Opera 8.0+, SafarixmlHttp=new XMLHttpRequest();}catch (e){// Internet Explorertry{xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");}catch (e){xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");}}return xmlHttp;}

Here's the getrss.php I'm using:

<?php//get the q parameter from URL$q = $_GET["q"];//find out which feed was selectedif ($q == 'Google') { $xml=("http://news.google.com/news?ned=us&topic=h&output=rss"); }elseif ($q == 'MSNBC') { $xml=("http://rss.msnbc.msn.com/id/3032091/device/rss/rss.xml"); }elseif ($q == 'WSFA') { $xml=("http://www.wsfa.com/Global/category.asp?C=62542&clienttype=rss"); }elseif ($q == 'FOX') { $xml=("http://www.foxnews.com/xmlfeed/rss/0,4313,0,00.rss"); }$xmlDoc = new DOMDocument();$xmlDoc->load($xml); //get elements from <channel>$channel=$xmlDoc->getElementsByTagName('channel')->item(0);$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue; //output elements from <channel>echo("<p><a href='" . $channel_link . "'>" . $channel_title . "</a>");echo("<br />");echo($channel_desc . "</p>"); //get and output"<item>" elements $x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) { $item_title=$x->item($i)->getElementsByTagName('title') ->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link') ->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description') ->item(0)->childNodes->item(0)->nodeValue; echo ("<p><a href='" . $item_link . "'>" . $item_title . "</a>"); echo ("<br />"); echo ($item_desc . "</p>"); }?>

Here's the HTML I'm using:

<form> Select a News Sorce:<select name="rss" onchange="showRSS(this.value)"><option>Select One</option><option value="FOX">FOX News</option><option value="WSFA">WSFA Local News</option><option value="Google">Google News</option><option value="MSNBC">MSNBC News</option></select></form><div id="rssOutput">News will display here.</div>

This is in the head tags:

<script type='text/javascript' src='includes/getrss.js'></script>

I have both the .js and .php in a folder called includes.

Link to comment
Share on other sites

Right, that looks like the same code you posted above. I don't see in this code what could be the problem. The error is indicating that the Javascript cannot find the element in the page. You posted the Javascript code, and you posted the HTML code for the form, but both of those look fine. There's nothing wrong with using document.getElementById like that (as far as I know), and obviously the element is below the table in the HTML. If that were all the code it should be working fine, but I want to see the entire page, exactly as you're running it. The code you posted is not the problem, as far as I can tell. If it's not working then something else on the page might be breaking it, but I can't tell that from the code you posted.

Link to comment
Share on other sites

I think I may have discovered part of the problem. In the head tag I am also calling the JavaScript - selectuser.js, which is from the PHP AJAX database tutorial on this website. By eliminating this I was able to move on to a new problem. When I click on a news link I get this:Parse error: parse error, unexpected T_OBJECT_OPERATOR in /local/home/user/www/includes/getrss.php on line 27This is that line:

$channel=$xmlDoc->getElementsByTagName('channel')->item(0);

When I drop the below to the next line the above error refers to it:

->item(0);

Link to comment
Share on other sites

From what I can tell from the documentation for DOM XML, everywhere you see something like this:$xmlDoc->getElementsByTagNameIt should be this:$xmlDoc->get_elements_by_tagnameThe function name in the sample code is wrong, it should be the other way.

Link to comment
Share on other sites

  • 1 year later...

My server says it is using PHP 5.2.9 and Im having the same problem:

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/website/public_html/getrss.php on line 21

This is line 21

$channel=$xmlDoc->getElementsByTagName('channel')->item(0);

I tried replacing getElementsByTagName with get_elements_by_tagname, still produces the same error.I also tried searching for an answer as well. No luck! Any PHP ninja/pirates that can hack and slash an appropriate fix?

Link to comment
Share on other sites

Try replacing this:$channel=$xmlDoc->getElementsByTagName('channel')->item(0);with this:$tmp=$xmlDoc->getElementsByTagName('channel');$channel = $tmp->item(0);If that works, try replacing the original line with this:$channel=($xmlDoc->getElementsByTagName('channel'))->item(0);

Link to comment
Share on other sites

Thank you for responding! I tried that and now I get

Parse error: syntax error, unexpected T_OBJECT_OPERATOR in /home/website/public_html/getrss.php on line 24

Line 24 is:

->item(0)->childNodes->item(0)->nodeValue;

so the //get elements from "<channel>" looks like this:

$tmp=$xmlDoc->getElementsByTagName('channel');$channel = $tmp->item(0);$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;$channel_link = $channel->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue;$channel_desc = $channel->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue;

Any other recommendations?

Link to comment
Share on other sites

Those should all be on the same line, there isn't a line break there. These:$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;are 1 line. I don't think that's the problem though, it's probably the same as above. You might need to do the same thing with the other lines, save each item that comes back until it gets to the one you want. So you would break up this line:$channel_title = $channel->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue;into something like this:$tmp1 = $channel->getElementsByTagName('title');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$channel_title = $tmp2->nodeValue;I'm not sure why it's an error when it's done the other way, that shouldn't be an error. This isn't the first time I've seen this though. You might actually just be able to use parentheses, try this version first and see if it works:$channel_title = (((($channel->getElementsByTagName('title'))->item(0))->childNodes)->item(0))->nodeValue;that way at least you could keep it on one line.

Link to comment
Share on other sites

Ok, I had to use the second piece of code to break it down and now it is skipping to line 52://get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) { $item_title=$x->item($i)->getElementsByTagName('title')->item(0)->childNodes->item(0)->nodeValue; $item_link=$x->item($i)->getElementsByTagName('link')->item(0)->childNodes->item(0)->nodeValue; $item_desc=$x->item($i)->getElementsByTagName('description')->item(0)->childNodes->item(0)->nodeValue; Now I need to break this apart so it should look something like this://get and output "<item>" elements$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) { $tmp1 = $x->getElementsByTagName('title');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_title = $tmp2->nodeValue; $tmp1 = $x->getElementsByTagName('link');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_link = $tmp2->nodeValue; $tmp1 = $x->getElementsByTagName('description');$tmp2 = $tmp1->item(0);$tmp1 = $tmp2->childNodes;$tmp2 = $tmp1->item(0);$item_desc = $tmp2->nodeValue;Right?Now I am getting this error:Warning: domdocument() expects at least 1 parameter, 0 given in /home/website/public_html/getrss.php on line 17Fatal error: Call to undefined function: load() in /home/website/public_html/getrss.php on line 18Line 17&18 are: $xmlDoc = new DOMDocument();$xmlDoc->load($xml);So this means the DOM is not loading? Do I have to break down this "$x=$xmlDoc->getElementsByTagName('item');for ($i=0; $i<=2; $i++) {" also? I was unsure how the for loop should be broken down while still allowing the statement to validate. Thanks for your help. I wish I understood coding a little better, but i am trying! =p

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...