I'm trying to make a table and I'm having problems with the edit button. I want to click on this button to load the data that is in the table to the form from the BD. So for this I want to send the string I get from the database with fetch to a javascript function:
$statement = $conexion->prepare('SELECT idMascota, Nombre_mascota, Tipo_mascota, Raza_mascota, Color_mascot, Foto_mascota, Residente_idResidente FROM mascota WHERE Residente_idResidente = :idResidente ');
$statement->execute(array(':idResidente' => $idResidente));
//print_r($statement->fetch(PDO::FETCH_ASSOC));/*regresa Array ( [idMascota] => 1 [Nombre_mascota] => Zeus [Tipo_mascota] => Perro [Raza_mascota] => French Poodle [Color_mascot] => Cafe [Foto_mascota] => (regresa la imagen en binario ���J...)
/* $row2 = $statement->fetch(PDO::FETCH_ASSOC);
echo json_encode($row_json); regreso null*/
while ($row = $statement->fetch(PDO::FETCH_ASSOC)) {//FETCH_ASSOC me regresa un array con todos los elementos
$mascota = array('idMascota' => $row['idMascota'],
'Nombre_mascota' => $row['Nombre_mascota'],
'Tipo_mascota' => $row['Tipo_mascota'],
'Raza_mascota' => $row['Raza_mascota'],
'Color_mascot' => $row['Color_mascot'],
'Foto_mascota' => base64_encode($row['Nombre_mascota']),);
?>
<tr>
<td><?php echo $row['Nombre_mascota'] ?></td><!--llamo al elemento Nombre_mascota-->
<td><?php echo $row['Tipo_mascota'] ?></td><!--llamo al elemento Tipo_mascota-->
<td><?php echo $row['Raza_mascota'] ?></td><!--llamo al elemento Raza_mascota-->
<td><?php echo $row['Color_mascot'] ?></td><!--llamo al elemento Color_mascot-->
<td><img height="70px" src="data:image/jpg;base64,<?php echo base64_encode($row['Foto_mascota']); ?>"/></td>
<td>
<button class="btn btn-warning glyphicon glyphicon-pencil" data-toggle="modal" data-target="#modalEdicion" onclick="agregaform('<?php echo json_encode($mascota); ?>')">
</button>
</td>
This is the javascript function:
function agregaform(datos){
var data = datos;
//d=datos.split('||');
$('#idMascota').val(data['idMascota']);
$('#nombreu').val(data['Nombre_mascota']);
$('#tipou').val(data['Tipo_mascota']);
$('#razau').val(data['Raza_mascota']);
$('#coloru').val(data['Color_mascot']);
}
The idea of sending the fix from php to javascript is that I want to click on the edit button to show me the data that is currently on the form. This is the table:
Clicking on the button shows this form (which is where I want to put the elements of the arrangement so that it is filled with the info that is already in the table)
I think where I am failing is to add the data to the form with javascript because I already get the data as in the next photo but nothing comes out in the form