By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,949 Members | 1,827 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,949 IT Pros & Developers. It's quick & easy.

problem with arrays

chelvan
P: 90
hi
forum



i've an array, which read the data values from the database. now i need to pass this array into javascript. is it possible? how is it?

thanks

chel-1
Sep 26 '08 #1
Share this Question
Share on Google+
4 Replies


ak1dnar
Expert 100+
P: 1,584
I think you need to pass generated php array elements to a javascript array right? Have a look here, Its just an example to do the job. Hope this helps.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lang = array("PHP", "C++", "Python");
  3. var_dump($lang);
  4. ?>
  5. <html>
  6. <head>
  7. <script type="text/javascript">
  8. function myFunction(){
  9. var myArray = new Array();
  10.  
  11.     <?php
  12.         foreach($lang as $key  => $value) {
  13.     ?>
  14.          myArray[<?php print $key?>] = "<?php print $value?>";
  15.  
  16.     <?php
  17.     }
  18.     ?>
  19.  
  20. for (var i=0; i<myArray.length; i++) {
  21.  alert(myArray[i]);
  22. }
  23. }
  24. </script>
  25.  
  26. </head>
  27.  
  28. <body onLoad="myFunction()">
  29. </body>
  30. </html>
  31.  
Sep 26 '08 #2

chelvan
P: 90
I think you need to pass generated php array elements to a javascript array right? Have a look here, Its just a example to do the job. Hope this helps.
Expand|Select|Wrap|Line Numbers
  1. <?php
  2. $lang = array("PHP", "C++", "Python");
  3. var_dump($lang);
  4. ?>
  5. <html>
  6. <head>
  7. <script type="text/javascript">
  8. function myFunction(){
  9. var myArray = new Array();
  10.  
  11.     <?php
  12.         foreach($lang as $key  => $value) {
  13.     ?>
  14.          myArray[<?php print $key?>] = "<?php print $value?>";
  15.  
  16.     <?php
  17.     }
  18.     ?>
  19.  
  20. for (var i=0; i<myArray.length; i++) {
  21.  alert(myArray[i]);
  22. }
  23. }
  24. </script>
  25.  
  26. </head>
  27.  
  28. <body onLoad="myFunction()">
  29. </body>
  30. </html>
  31.  
thanks its ok.
but if i create the array from reading the database what will happen. is the above code is ok or am i need to change?

chel-1
Sep 26 '08 #3

ak1dnar
Expert 100+
P: 1,584
As I said Its an example to do the job. You have to do the changes as required. Good luck!
Sep 26 '08 #4

chelvan
P: 90
As I said Its an example to do the job. You have to do the changes as required. Good luck!

thanks ak1dnar.
i try and come back again.



thanks
chel-1
Sep 26 '08 #5

Post your reply

Sign in to post your reply or Sign up for a free account.