ginerjm Posted April 14, 2011 Share Posted April 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 And Yes - I have tried using a reference to $jsgolfers as well as just plain jsgolfers. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2011 Share Posted April 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 Lifted from examples on "helpful" web pages. I had to modify that just to get a response out of it - which took me hours! Quote Link to comment Share on other sites More sharing options...
gizmola Posted April 14, 2011 Share Posted April 14, 2011 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. Quote Link to comment Share on other sites More sharing options...
ginerjm Posted April 14, 2011 Author Share Posted April 14, 2011 MUCH better. Thanks! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.