Forum Moderators: coopster

Message Too Old, No Replies

Change One Word of a Javascript Code with PHP?

How Can I Use a Variable in the URL to Change a News Feed?

         

clearpixel

12:57 am on May 17, 2006 (gmt 0)

10+ Year Member



I recently posted a similar question but for a slightly different goal. However, in this case I would like to use PHP to change one word in a Javascript code I use to import an RSS news feed onto a web page. I want to be able to control which word appears with a variable in the URL I use to get to the page in question. The one word - IN CAPS in the snippet of relevant code below - is the keyword that generates the subject of the news articles. I envision using this for a shared "coming soon" page for several domains I own. Here's the code:

<script language="JavaScript" src="http://www.mydomain.com/feed/feed2js.php? src=http%3A%2F%2Fnews.google.com%2Fnews%3Fhl%3Den%26ned %3Dus%26q%3DSUBJECT%26output%3Drss&num=5&desc=1" type="text/javascript"></script>

Can this be done?

[edited by: jatar_k at 4:01 pm (utc) on May 17, 2006]
[edit reason] fixed sidescroll [/edit]

eelixduppy

1:05 am on May 17, 2006 (gmt 0)



echo "<script language='JavaScript' src=\"http://www.mydomain.com/feed/feed2js.php? src=http%3A%2F%2Fnews.google.com%2Fnews%3Fhl%3Den%26ned %3Dus%26q%3D".$_GET['SUBJECT']."%26output%3Drss&num=5&desc=1\" type=\"text/javascript\"></script>";

I hope this is what you mean.

[edited by: jatar_k at 4:01 pm (utc) on May 17, 2006]
[edit reason] fixed sidescroll [/edit]

clearpixel

1:15 am on May 17, 2006 (gmt 0)

10+ Year Member



That could very well be what I mean (I still need to test it). How would I call it in the URL (e.g. what is the variable name - SUBJECT?)

[edited by: clearpixel at 1:18 am (utc) on May 17, 2006]

eelixduppy

1:17 am on May 17, 2006 (gmt 0)

clearpixel

1:21 am on May 17, 2006 (gmt 0)

10+ Year Member



I am getting a syntax error with the Javascript...seems to be referencing the actual URL?

EDIT: Nevermind....I forgot to change the URL in the code from "mydomain" to my actual domain. Now it works perfectly!

clearpixel

6:57 pm on Jun 5, 2006 (gmt 0)

10+ Year Member



How would I modify the code so that if there is no variable specified in the URL I can 1.) Indicate a default or 2.) Not show the news feed at all or 3.) Replace the news feed with an external text file?

jatar_k

10:55 pm on Jun 5, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you would just need to test for isset or empty on the GET var

using subject as the varname

$subject = '';
if (!isset($_GET['subject']) ¦¦ empty($_GET['subject'])) $subject = 'mydefault';
else $subject = $_GET['subject'];

you could do whatever you want in the if, this just sets a default value

NOTE: WebmasterWorld breaks pipe chars ¦, replace all such characters with real pipe characters for copied code to work