0

Well i have this php for get the value of mysql. I get two rows in this query.

include('../funciones/funciones.php');
$link=Conectarse();
$pnc=$_GET['pnc'];
$query="SELECT
accion_inmediata.accion,
accion_inmediata.responsable,
accion_inmediata.peligro,
accion_inmediata.ambiente,
DATE_FORMAT(accion_inmediata.fecha,'%m-%d-%Y') as fechaAccion
FROM
accion_inmediata
WHERE
accion_inmediata.id_pnc = '$pnc'";
$result = mysql_query($query,$link);
$array = mysql_fetch_array($result);
echo json_encode($array);

and this is my Jquery code

function traerAcciones(pnc){

      $.ajax({                         
      url: 'php_ajax/select_acciones.php?pnc='+pnc,               
      data: "",                        
      dataType: 'json',                
      success: function(data)          
      {

  $.each(data, function() {
 alert(this.accion);

});
      } 
    }); 
    }

when i execute this code the alert show me "undefined". The $.each loop works fine but the value is the problem. Please help and sorry for my bad English

4
  • data is empty for any particularx reason? Commented Nov 11, 2013 at 20:16
  • set php page handler as json - header('Content-Type: application/json'); Commented Nov 11, 2013 at 20:16
  • go in your browser console, look for request in network tab and copy the response sent...show what that is Commented Nov 11, 2013 at 20:16
  • also...try alert this without .accion Commented Nov 11, 2013 at 20:20

2 Answers 2

2

Try to pass the value and the Index as parameters, this is how it works:

$.each(data, function(index, value) {
 alert( value.accion );
});

Might also be a problem with the data object not containing what you think it does, do a

console.log(index, value);

inside your loop and a

console.log(data);

before the loop and you will see what you have and how to handle it.

Hope it helps!

Sign up to request clarification or add additional context in comments.

2 Comments

i get undefined too :/
with console.log(index, value); i get 0 ACCION funciones.js:412 1 RESPON funciones.js:412 2 NO funciones.js:412 3 SI funciones.js:412 4 11-11-2013 funciones.js:412 accion ACCION funciones.js:412 responsable RESPON funciones.js:412 peligro NO funciones.js:412 ambiente SI funciones.js:412 fechaAccion 11-11-2013 and with console.log(data); i get Object {0: "ACCION", 1: "RESPON ", 2: "NO", 3: "SI", 4: "11-11-2013", accion: "ACCION", responsable: "RESPON ", peligro: "NO", ambiente: "SI", fechaAccion: "11-11-2013"}
0

Currently you are returning the array of your first row only. So to retrieve both you might want to do this:

include('../funciones/funciones.php');
$link=Conectarse();
$pnc=$_GET['pnc'];
$query="SELECT accion_inmediata.accion, accion_inmediata.responsable, accion_inmediata.peligro, accion_inmediata.ambiente, DATE_FORMAT(accion_inmediata.fecha,'%m-%d-%Y') as fechaAccion FROM accion_inmediata WHERE accion_inmediata.id_pnc = '$pnc'";
$result = mysql_query($query,$link);
$array = array();
while($row = mysql_fetch_array()):
$array[] = $row;
endwhile;
echo json_encode($array);

Now your jquery should be able to retrieve the data. Also, please avoid using MYSQL_QUERY it's deprecated.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.