I would like to know how to pass the associative array $data[] to a javascript function within a <button>
without having to place it every time $data[]
, that is: First I do my query
<?php
$resultado=mvcontrolador::listadoTablaControlador("t_cliente",null);
foreach ($resultado as $indice => $data) { ?>
I display the fields in a table:
<tr id="campos">
<td><?php echo $data['nombre_cliente'];?></td>
<td><?php echo $data['apellido_cliente'];?></td>
<td><?php echo $data['cedula_cliente'];?></td>
<td><?php echo $data['correo_cliente'];?></td>
<td><?php echo $data['telf_cliente'];?></td>
<td><?php echo $data['estado'];?></td>
<td><?php echo $data['codigopostal'];?></td>
<td><?php echo $data['localidad'];?></td>
Y donde quiero que dichos valores del array sean insertados en una funcion
dentro de un
`
<form method="POST">
<td> <button type="button" value=""
onclick='return verInfoCliente("<?php echo $data['id_cliente'];?>");'
name="infoCliente" >Info</button></td> <?php } ?>
What I want to do now is use inside a function that is in a separate js file:
function viewCustomerInfo(){}
That shows an alert with all the data of my array without having to manually place all the data of the array separately. Without having to do this:
verInfoCliente("<?php echo $data['id_cliente'],
$data['nombre_cliente'],
$data['apellido_cliente'],
$data['cedula_cliente'],
$data['correo_cliente'],
$data['telf_cliente'];?>");
And the javascript function:
function verinfocliente(id,nombre,apellido,cedula,correo,telf){
alert('Los datos del cliente son: \nNombre:'+nombre+'\nApe... ');
}
etc...
I want is to pass that PHP array to the funcion()
javascript and within that funcion()
use that array in my alert()
.
I hope that it is understood what I want to expose, thanks in advance.
You can't directly pass it from array
php
to arrayjavascript
, but you can pass it directly from arrayphp
tojson
, that is, anobjeto json
injavascript
.In PHP apply the function
json_encode
to the array.According to your code it would be:
Function
javascript
receives the object, values are obtained by interpreting thejson
:Finally if you want it to be strictly a
array
ofjavascript
(but not ajson
) you should create a custom function inphp
, but at this point it is not necessary to complicate. LuckYou can define the variables like this in JS so you don't have to call PHP every time:
This is how my function looked when clicking on
<button type="button" onclick="verInfoCliente("<?php echo json_encode($data);?>")">
:function verInfoCliente(datos){ if (confirm('Infomacion del Cliente. \nNombres: '+datos.nombre_cliente+ '\nApellidos: '+datos.apellido_cliente+ '\nCedula: '+datos.cedula_cliente+ '\nCorreo: '+datos.correo_cliente+ '\nTelefono: '+datos.telf_cliente+ '\nEstado: '+datos.estado+ '\nCodigo Postal: '+datos.codigopostal+ '\nLocalidad: '+datos.localidad+ '\nFecha de registro: '+datos.fecha_reg+ '\n\n¿Desea editar este cliente?')) { alert('Asegurese de que los datos sean los correctos.'); window.location.href="editarCliente&idCliente="+datos.id_cliente; }else{ return false; } }
Graphically it shows me this:Thanks everyone for your help!