7 Replies - 1357 Views - Last Post: 11 September 2009 - 07:50 AM

#1 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Passing php array to javascript

Posted 09 September 2009 - 12:36 PM

What methods can I implement to pass a php array into a Javascript array? without know the size of the array.

If I know the size of the array It would be easier but the size will vary.

Another method I was thinking about was to save the contents to a file and then read that file in with Javascript, but that seems a bit slow.

Sorry if this is in the wrong forum, not sure if it should be in php or Javascript?

Thank you for your time.
Musya

This post has been edited by musya: 09 September 2009 - 12:36 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Passing php array to javascript

#2 BetaWar  Icon User is offline

  • #include "soul.h"
  • member icon

Reputation: 1147
  • View blog
  • Posts: 7,133
  • Joined: 07-September 06

Re: Passing php array to javascript

Posted 09 September 2009 - 12:56 PM

I would do something like so:

<?php
$arr = array(0, 1, 2, 3, 4, 5, 6);
echo "<script>var array = new Array();";
foreach($arr AS $key => $val){
  echo "array[] = " . $val . "; ";
}
echo "alert(array.length); </script>";
?>


Hope that helps
Was This Post Helpful? 1
  • +
  • -

#3 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Passing php array to javascript

Posted 09 September 2009 - 01:00 PM

Yea it does, thank you. I forgot you can echo javascript code in php...duhh.
Was This Post Helpful? 0
  • +
  • -

#4 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Passing php array to javascript

Posted 09 September 2009 - 01:13 PM

I got another problem now, thought I would use the same post

here is the code
<script language="javascript" type="text/javascript"> 
var feedInfo = new Array();
var count = 0;
<?
for($i = 0; $i < sizeof($feed); $i++)
{
	echo "feedInfo[".$i."] = '".$feed[$i]."'";
}
?>

function next()
{
	count++;
	document.getElementById("feed").innerHTML = feedInfo[count];
}
</script>


<span id="feed">hello</span>

<?
echo '<span style="position:relative;left:300px;"><img onclick="next();" src="images/content/nextButton.jpg" width=24 height=26 vspace=0 hspace=0></span>';
?>



The array gets allocated fine, but when i click on the image it doesn't do anything, the next(); doesn't work. IE says object expected. Its suppose to just change the content of "feed" with the next element in the array.
Was This Post Helpful? 0
  • +
  • -

#5 forest51690  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 58
  • View blog
  • Posts: 340
  • Joined: 20-March 09

Re: Passing php array to javascript

Posted 09 September 2009 - 06:54 PM

maybe next() is a reserved word. Try naming it something else.
Was This Post Helpful? 0
  • +
  • -

#6 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Passing php array to javascript

Posted 10 September 2009 - 11:02 AM

No I tried a different name still nothing, I even tried putting the onclick="" in an <a href=""></a> tag but still nothing.
Was This Post Helpful? 0
  • +
  • -

#7 ahmad_511  Icon User is offline

  • MSX
  • member icon

Reputation: 131
  • View blog
  • Posts: 722
  • Joined: 28-April 07

Re: Passing php array to javascript

Posted 10 September 2009 - 01:28 PM

Hello,
1-you did use the short open tag <? ?>in your last post, so make sure the short_open_tag is enabled in php.ini
other wise use the most common one <?php ?>

2- when i tried it on IE i got (Expected ; ) error message, and it works fine after i added the (; ) to the array feeding code
echo "feedInfo[".$i."] = '".$feed[$i]."';";



without (; ) the generated script will be something like:
feedInfo[0] = 'val0'feedInfo[1] = 'val1'feedInfo[2] = 'val2'
and of course this will raise an javascript error

hope it helps

This post has been edited by ahmad_511: 10 September 2009 - 01:30 PM

Was This Post Helpful? 0
  • +
  • -

#8 musya  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 11
  • View blog
  • Posts: 1,012
  • Joined: 25-April 07

Re: Passing php array to javascript

Posted 11 September 2009 - 07:50 AM

Yes I caught that, thank you. stupid mistakes.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1