Passing PHP Variable to JS Var within a HereDoc

Soldato
Joined
26 Aug 2012
Posts
4,399
Location
North West
Hi, I have a foreach loop, roughly as shown below, excluding the unimportant stuff.

I am trying to pass to PHP variables based on the progress within the foreach loop, to a JavaScript function. The variables get passed right to the HTML feed,just not the JS function. The variables I need to pass to the js function are $feed->name and $feed->URL.

I read up elsewhere that the best way to pass these to the JS function as parameters is to set them as JS variables first and then use these as parameters to the function.

So in this case I need to set
Code:
<script>
var feedName=<?php echo $feed->name ;?>
var feedUrl= <?php echo $feed->url ;?>
</script>

However, I just can't get this to work within the heredoc, no matter what I've tried which i've found online, as obviously it gets processed as the literal value rather than a variable. I am not very knowledgeable in JS, just to let you know.

Any help would be more appreciated.

PHP:
	foreach($rss_feeds as $feed)
	{		
		 echo <<< HTML
			<tr>
				<td><form action="router.php" method="post">
					<input name="edit-feed" type="hidden" value="$feed->id" />
					<input name="edit-feed-submit" type="submit" value="Edit" class="edit-feed-button" onclick="javascript:modify_feed(feedName,feedUrl);" /></form>
				</td>
				
			</tr>
HTML;
	}
 
Last edited:
Soldato
OP
Joined
26 Aug 2012
Posts
4,399
Location
North West
Wouldn't it just be the case of:

PHP:
onclick="javascript:modify_feed('$feed->name','$feed->url');"

http://3v4l.org/ZFBnS

That is what I originally assumed, but it isn't working within the heredoc.

^^ This would be my preference - not really sure what the point of the javascript variables are in this instance.

I am using a JS function which gives the user a prompt where they can edit the variable, based on which item they clicked.
 
Back
Top Bottom