3 Replies - 2084 Views - Last Post: 20 October 2010 - 04:29 PM Rate Topic: -----

#1 althejandro  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 29-June 10

assign PHP variable into JS variable not working

Posted 20 October 2010 - 02:35 PM

I am trying to assign the value $grabbedFeed to the JS variable feedurl
So far, everything else is working. If I echo out $grabbedFeed without the JS the while loop with grab all the values of the feed(x) columns and is successfully copying the JS declarations

right now when I view the source of the code being run on the web it is showing feedurl = "$grabbedFeed"


Here is an excerpt of the code





function rssfeedsetup(){
var feedpointer=new google.feeds.Feed(feedurl) //Google Feed API method
feedpointer.setNumEntries(feedlimit) //Google Feed API method
feedpointer.load(displayfeed) //Google Feed API method
}

function displayfeed(result){
if (!result.error){
var thefeeds=result.feed.entries
for (var i=0; i<thefeeds.length; i++)
rssoutput+="<li><a href='" + thefeeds[i].link + "' target='_blank'>" + thefeeds[i].title + "</a></li>"
rssoutput+="</ul>"
feedcontainer.innerHTML=rssoutput
}
else
alert("Error fetching feeds!")
}

</script>
<script type="text/javascript">
google.load("feeds", "1") //Load Google Ajax Feed API (version 1)
</script>
</head>
<body>

<h3>You're Logged In</h3>
<h6>Hello, <?php echo $_SESSION['user'];?> </h6>


<?php

$oldFeedNumber = mysql_query ("SELECT numberOfF FROM users WHERE username = '".$_SESSION['user']."'") or die(mysql_error());
while($row = mysql_fetch_array($oldFeedNumber))
{
$feedNumber= $row['numberOfF'];
}


	$x=0;
	
	while($x<=$feedNumber)
	{
	$string = feed;
	$feedV = $string.$x;
        $grabFeed = mysql_query ("SELECT $feedV FROM users WHERE username = '".$_SESSION['user']."'") or die(mysql_error());
	$fArray = mysql_fetch_row($grabFeed);
	$grabbedFeed = $fArray[0];
	
	?>
	
	<script type="text/javascript">
var feedcontainer=document.getElementById("feeddiv")
<?php echo 'var feedurl= "$grabbedFeed"';?>
var feedlimit=10
var rssoutput="<b>Your News:</b><br /><ul>"

window.onload=function(){
rssfeedsetup()
}

</script>

	
	<?php $x=$x+1;} ?>




Is This A Good Question/Topic? 0
  • +

Replies To: assign PHP variable into JS variable not working

#2 creativecoding  Icon User is offline

  • Hash != Encryption
  • member icon


Reputation: 927
  • View blog
  • Posts: 3,209
  • Joined: 19-January 10

Re: assign PHP variable into JS variable not working

Posted 20 October 2010 - 03:44 PM

Remove the quotes.
Was This Post Helpful? 0
  • +
  • -

#3 CTphpnwb  Icon User is online

  • D.I.C Lover
  • member icon

Reputation: 3037
  • View blog
  • Posts: 10,615
  • Joined: 08-August 08

Re: assign PHP variable into JS variable not working

Posted 20 October 2010 - 04:05 PM

Remove the single quotes from around the variable:

<?php echo 'var feedurl= "'.$grabbedFeed.'"'; ?>



You have three languages in one snippet of code! No wonder you're having trouble finding the problem. Separate your code.
Was This Post Helpful? 0
  • +
  • -

#4 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: assign PHP variable into JS variable not working

Posted 20 October 2010 - 04:29 PM

I usually do it like :

   var feedurl = "<?php echo $grabbedFeed; ?>";


This post has been edited by Jstall: 20 October 2010 - 04:30 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1