Jump to content

PHP arrays into JS


ginerjm

Recommended Posts

Been reading all afternoon.  Here is where I am at with ONE big error.  Everytime I call the function 'gn(n)' I get the message that "jsgolfers is undefined'.

 

echo '<script type="text/javascript">';
echo 'var jsgolfers = new array["<?php echo join(":\", $golfers); ?>"];';
echo '</script>';

echo '<script type="text/javascript">';
echo 'function gn(str)';
echo '{';
echo 'alert(jsgolfers[$str]);';
echo '}';
echo '</script>';

 

First - the $golfers array is created and has values which I've displayed on my page

 

I've had a lot of difficulty with the join function - the example I found on the web didn't work so I've whittled it down to what it is.

 

Will appreciate any pointers one can provide. Supposedly this code will take a php-produced array var and build a JS array var for me to use in an onclick event on my page.

Link to comment
https://forums.phpfreaks.com/topic/233758-php-arrays-into-js/
Share on other sites

Eureka!  After dumping my generated html page to an editor I saw that someone (php? js?) was inserting error messages (<a .....> even) into my html and that was causing a lot of trouble apparently.

 

I massaged my code and this now does what I expect.

echo '<script type="text/javascript">';
?>
var jsgolfers = ["<?php echo implode("\",\"",$golfers); ?>"];
<?php
echo 'function gn(str)';
echo '{';
echo 'alert(jsgolfers[str]);';
echo '}';
echo '</script>';

 

Basically - the $golfers array is built using php and sql early in my script.  then the js creates a global array and loads it from $golfers.  Then my onclick attribute on a d/e field calls the function which properly displays the corresponding golfer depending upon the input I entered.

 

Link to comment
https://forums.phpfreaks.com/topic/233758-php-arrays-into-js/#findComment-1201767
Share on other sites

As an aside, when I need to output js from php I will either include it, or use a Heredoc. 

 

The code you provided is not valid php, so I'm not confident in answering your question, when I don't know what the real code is.  For example, you have echo statements (php) and then you start a new php section right in the middle of what is already php.  That code would cause an error.

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/233758-php-arrays-into-js/#findComment-1201768
Share on other sites

Here's a heredoc.  Heredocs interpolate variables in the same way that using the double quotes does.

 

$golfers = implode("\",\"",$golfers);
$js =<br />
  var jsgolfers = ["$golfers"];<br />
  <br />
  function gn(str)<br />
	{<br />
	   alert(jsgolfers[str]);<br />
	}<br />

JS;

echo $js;

 

Eliminates all the annoying quoting, and escaping etc.

Link to comment
https://forums.phpfreaks.com/topic/233758-php-arrays-into-js/#findComment-1201771
Share on other sites

Archived

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

×
×
  • 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.