Foros del Web » Programando para Internet » Javascript »

[SOLUCIONADO] Mostrar array JSON en celda de tabla

Estas en el tema de Mostrar array JSON en celda de tabla en el foro de Javascript en Foros del Web. Buenass, quiero mostrar un array JSON en distintas celdas d una tabla que tengo creada. Para ello he creado mi archivo questionsolution.php donde creo el ...
  #1 (permalink)  
Antiguo 24/07/2013, 07:05
 
Fecha de Ingreso: junio-2013
Ubicación: Madrid
Mensajes: 61
Antigüedad: 10 años, 10 meses
Puntos: 5
Mostrar array JSON en celda de tabla

Buenass,

quiero mostrar un array JSON en distintas celdas d una tabla que tengo creada. Para ello he creado mi archivo questionsolution.php donde creo el array json y he comprobado q si ejecuto ese archivo lo imprime correctamente. El problema es llevarlo a donde quiero y que vaya cambiando en función d lo q seleccione el usuario, para ello:

he creado otro archivo donde he creado una función GetData la cual se supone que detecta si el usuario ha seleccionado algo o no:

Código Javascript:
Ver original
  1. function GetData() {
  2.     if (window.XMLHttpRequest) {// code for IE7+, Firefox, Chrome, Opera, Safari
  3.         xmlhttp=new XMLHttpRequest();
  4.     } else {// code for IE6, IE5
  5.         xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  6.     }
  7.     xmlhttp.onreadystatechange=function() {
  8.         if (xmlhttp.readyState==4 && xmlhttp.status==200) {
  9.             //document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
  10.         }
  11.     };
  12.     xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
  13.    
  14.     var strSend = '';
  15.    
  16.     alert(oForm.elements['CountryInput'].value);
  17.     if ( oForm.elements['CountryInput'].value != '') {
  18.         strSend += '&CountryInput=' + oForm.elements['CountryInput'].value;
  19.     }
  20.    
  21.     alert(oForm.elements['CategoryInput'].value);
  22.     if ( oForm.elements['CategoryInput'].value != '') {
  23.         strSend += '&CategoryInput=' +    oForm.elements['CategoryInput'].value;
  24.     }
  25.         xmlhttp.open("POST","questionsolution.php",false);
  26.     xmlhttp.send(substring(strSend,2));
  27.  
  28.     var $JSONArray = JSON.parse(xmlhttp.responseText);
  29.    
  30.     return $JSONArray;
  31. }

Esto es parte del código dle label y el select, para el category es lo mismo:

Código HTML:
Ver original
  1. <label for="CountryInput"><?php echo $arrMainPage[$MainPageCountry]["TextHeader"]; ?></label>
  2.                                 <select name="CountryInput">
  3.                                     <option><?php echo $arrMainPage[$MainPageSelectOne]["TextHeader"]; ?></option>
  4.                                     <?php
  5.                                     $rcsCountry = $cardata->query($qryCountry);
  6.  
  7.                                     while ($row = $rcsCountry->fetch_array(MYSQLI_BOTH)) {
  8.                                         ?><Option><?php echo $row["CountryDescription"]; ?></option>
  9.                                     <?php
  10.                                     }
  11.                                    
  12.                                     $rcsCountry->free_result();
  13.                                     ?>                     
  14.                                 </select>

Luego he creado la tabla en la que se supone que aparecen los datos d mi array pero no consigo que funcione:

Código HTML:
Ver original
  1. <div class="listcars">
  2.                     <table id="boxcarwidth" >
  3.                         <colgroup id="picturecartable" />
  4.                         <tr>
  5.                             <td rowspan="7" colspan="2">Category</td>
  6.                             <td class="titlestable" ></td>
  7.                             <td class="titlestable" >Country</td>
  8.                             <td class="titlestable" ></td>
  9.                             <td class="titlestable" ></td>                         
  10.                         </tr>
  11.                     </table>
  12.                 </div>
Código Javascript:
Ver original
  1. <script type="text/javascript">
  2.                  
  3.                     $(document).ready(function(){
  4.                         //var url="http://localhost/questionsolution.php";
  5.                         //$("#boxcarwidth tbody").html("");
  6.                         var arrCar = [];
  7.                         arrCar = GetData();
  8.                         $.each(arrCar, function(key, value){
  9.                         //for (var i = 1; i < $arrCar; i++) {
  10.                             var tblRow =
  11.                                 "<tr>"
  12.                                     +"<td>"+arrCar[key].Category+"</td>"
  13.                                     +"<td>""</td>"
  14.                                     +"<td>"+arrCar[key].Country+"</td>"
  15.                                     +"<td>""</td>"
  16.                                     +"<td>""</td>"
  17.                                 +"</tr>" ;
  18.                                 //$(tblRow).appendTo("#boxcarwidth tbody");
  19.                                 $('#boxcarwidth').append(htm);
  20.                         });
  21.                     });
  22.                 </script>

Este es mi array d json:

Código PHP:
Ver original
  1. $rcsGetArray = $cardata->query($qryCarInformation);
  2.    
  3.     $RowNumber = 1;
  4.    
  5.     while ($row = $rcsGetArray->fetch_array(MYSQLI_BOTH)) {
  6.        
  7.         $Result[$RowNumber] = $row ;
  8.         $RowNumber += 1;
  9.     }
  10.    
  11.     $rcsGetArray->free_result();
  12.    
  13.     echo json_encode($Result);

el array de json me imrpime lo quiero q me imprima si ejecuto el archivo por separado, el problema es q no me aparece en la tabla lo que quiero que me aparezca, de hecho me aparece vacía.


Alguna sugerencia??

Muchas gracias d antemano.

Etiquetas: celda, funcion, html, input, js, json, php, select, tabla
Atención: Estás leyendo un tema que no tiene actividad desde hace más de 6 MESES, te recomendamos abrir un Nuevo tema en lugar de responder al actual.
Respuesta




La zona horaria es GMT -6. Ahora son las 03:32.