Ver Mensaje Individual
  #2 (permalink)  
Antiguo 06/04/2012, 10:10
Avatar de gjx2
gjx2
 
Fecha de Ingreso: agosto-2008
Ubicación: R.D
Mensajes: 1.153
Antigüedad: 15 años, 8 meses
Puntos: 139
Respuesta: Cómo pasar de php a array de javascript

Se me ocurren tres forma ahora mismo.

1- Hacerlo por ajax

2- ingresar los datos a la variable de js desde php

Código HTML:
Ver original
  1. <script type="text/javascript">
  2.  
  3. var img = new Array();
  4.  
  5. window.onload = function(){
  6. <?PHP
  7.         $i=0;
  8.         while($i<10){
  9.            
  10.             echo "img[".$i."]=".$i.";\n";
  11.        
  12.         $i++;  
  13.         }
  14.    
  15.    
  16.     ?>
  17. alert("Segunda forma" + img.length);
  18.  
  19. }
  20.  


3- Crear una cadena dividida por comas y en javascript haces uso de split ej.

Código HTML:
Ver original
  1. <?PHP
  2.         $i=0;
  3.         $img = "";
  4.         while($i<10){
  5.            
  6.             $img .= $i.",";
  7.        
  8.         $i++;  
  9.         }
  10.    
  11.     $img = trim($img,",");
  12.     ?>
  13.  
  14.  
  15. <script type="text/javascript">
  16.  
  17. var img = new Array();
  18.  
  19. window.onload = function(){
  20.  
  21.  
  22.  
  23.    
  24.     var t = "<?=$img?>";
  25.      img =  t.split(",");
  26.        
  27.        
  28.         alert("Tercera forma" + img.length);
  29.  
  30.  
  31. }
  32.  

Trabaja con la que mas te guste.