Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

PHP Forum


You are currently viewing our PHP Forum as a guest. Please register to participate.
Login



Freelance Jobs

Reply
PHP array to JS array
Old 08-09-2010, 03:52 PM PHP array to JS array
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
HI all

How do i pass a php array into a JS function? ( if its possible)

Cheers
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
 
Register now for full access!
Old 08-09-2010, 04:06 PM Re: PHP array to JS array
chrishirst's Avatar
Missing! presumed drunk.

Posts: 42,384
Name: Chris Hirst
Location: Blackpool. UK
Trades: 0
echo() the values into the source code.
__________________
Chris. ->>
Please login or register to view this content. Registration is FREE
<<-

A foolish consistency is the hobgoblin of little minds
Thought for today:- Is SEO the only industry where all the cowboys are Indians?
chrishirst is offline
Reply With Quote
View Public Profile Visit chrishirst's homepage!
 
Old 08-09-2010, 04:14 PM Re: PHP array to JS array
numbenator's Avatar
Webmaster Talker

Posts: 523
Location: London
Trades: 0
Well if i do the below

<?php // set array here ?>
<a href="" conclick="setarray(<?php echo arrayName; ?>; return false">click me</a>

<script>
function setarray(arrayname) {



}
</script>


I can't there after access the JS array
__________________

Please login or register to view this content. Registration is FREE
numbenator is offline
Reply With Quote
View Public Profile Visit numbenator's homepage!
 
Old 08-09-2010, 08:52 PM Re: PHP array to JS array
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
Try <?php echo '['.implode('][',$arrayName).']';?>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-10-2010, 01:59 AM Re: PHP array to JS array
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
I am not sure about passing an PHP array into a JS function, instead you shall construct a string with array values in PHP and pass it to JS function and in JS split it and use it.
Quote:
<?php
$arr = array('apple','orange','guava','jackfruit','lemon' ,'banana');
$str = implode('||',$arr);
?>
<a href="" onclick="setarray('<?php echo str ; ?>');">click me</a>

<script>
function setarray(arrayname) {

var s = arrayname.split('||');
for (i =0;i<s.length;i++)
{
alert(s[i]);
}
return false;

}
</script>
__________________
There is no secret ingredient.
phpdasan is offline
Reply With Quote
View Public Profile
 
Old 08-10-2010, 02:13 AM Re: PHP array to JS array
JeremyMiller's Avatar
WT Moderator

Posts: 1,712
Name: Jeremy Miller
Location: Las Vegas, NV
Trades: 0
OK. There were some typos. This demos it, though:

PHP Code:
<?php
$arrayName 
= array('a','b','c','d');
?><html>
  <head>
    <title>Demo</title>
    <script>
      function showArray(src_array) {
        for(var i=0;i<src_array.length;i++) {
          document.getElementById('MyArray').innerHTML += "<p>The " + i + "th element is equal to &quot;" + src_array[i] + "&quot;</p>";
        }
      }
    </script>
    <style>
      div.clickable {
        cursor:pointer;
        color:#00f;
      }
      div:hover.clickable {
        text-decoration:underline;
      }
    </style>
  </head>
  <body>
    <div onClick="showArray(<?php echo "['".implode("','",$arrayName)."']";?>);" class="clickable">Show My Array</div>
    <div id="MyArray"></div>
  </body>
</html>
__________________
Jeremy Miller

Please login or register to view this content. Registration is FREE
JeremyMiller is offline
Reply With Quote
View Public Profile Visit JeremyMiller's homepage!
 
Old 08-10-2010, 04:06 AM Re: PHP array to JS array
Experienced Talker

Posts: 44
Trades: 0
I always use json_encode() as it's easily read in JavaScript
Onkel E is offline
Reply With Quote
View Public Profile
 
Old 08-10-2010, 06:31 AM Re: PHP array to JS array
phpdasan's Avatar
Experienced Talker

Posts: 37
Name: Karthick B
Trades: 0
Yes, using json is another option, here is a sample code

PHP Code:

<?PHP

$arr 
= array('one' => 'orange''two' => 'apple''three' => 'peach''four' => 'rose');

$str = (json_encode($arr));    
    
?>

<script>
var s = '<?PHP echo $str?>';
var obj = eval("(" + s + ")");
alert(obj.one);
</script>
But json only available since PHP 5.2.0
__________________
There is no secret ingredient.

Last edited by phpdasan; 08-10-2010 at 06:33 AM..
phpdasan is offline
Reply With Quote
View Public Profile
 
Reply     « Reply to PHP array to JS array
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.44490 seconds with 12 queries