How can I go through the rows of an HTML table and get the values of a cell with a button?
I am making an application in php and I need to send some data obtained from a table through ajax to another php file. I have already obtained the value of a specific cell, the problem is that when I send it, I am not receiving one of the data specifically:
idcita = $(this).html() + "\n";
This is the error that shows me:
Notice: Undefined index: idcita in C: \ xampp \ htdocs \ uci_citas \ model_table.php on line 3
These are the files I'm handling:
Script:
<script>
$(document).ready(function() {
$('.enviar').on('click', function() {
/*var tags_td = new Array();
var tags_td=document.getElementsByName('n');
var att = document.getElementsByName('atendido');
var celda = 0;*/
var idcita = "";
var atendido = $("#atendido").val();
// Obtenemos todos los valores contenidos en los <td> de la fila
// seleccionada
$(this).parents("tr").find(".n").each(function() {
idcita = $(this).html() + "\n";
});
console.log(idcita);
$.ajax({
data: {idcita:idcita, atendido:atendido}, //datos que se envian a traves de ajax
url: 'modelo_tabla.php', //archivo que recibe la peticion
type: 'POST', //método de envio
success: function (response) {
location.href='citas.php';
}
});
});
});
</script>
functions.php:
<?php
function tabla(){
try {
include_once("conexion.php");
$fecha = date('Y-m-d');
$stmt = $con_bd->prepare('SET max_join_size=18446744073709551615');
$stmt->execute();
$stmt = $con_bd->prepare('SELECT * FROM solicitar_cita WHERE dia = :dia ORDER BY hora ASC');
$stmt->bindParam(':dia', $fecha);
$stmt->execute();
$rs = $stmt->fetchAll();
$i = -1;
if (!empty($rs)) {
$tabla = "<table class='table table-hover table-responsive' id='tabla_datos'>
<thead>
<tr class='bg-primary'>
<th scope='col'>ID</th>
<th scope='col'>Nombre</th>
<th scope='col'>Apellido</th>
<th scope='col'>Identificacion</th>
<th scope='col'>Celular</th>
<th scope='col'>Direccion</th>
<th scope='col'>Correo</th>
<th scope='col'>Hora</th>
<th scope='col'>Atendido</th>
<th scope='col'>Boton</th>
</tr>
</thead>";
"<tbody>";
foreach ($rs as $resultado) {
$i = $i+1;
$val = ($resultado['atendido'] == 1) ? 1 : 1;
$tabla .= "<tr>
<td class='n'>".$resultado['idsolicitar_cita']."</td>
<td>".$resultado['nombre']."</td>
<td>".$resultado['apellido']."</td>
<td>".$resultado['identificacion']."</td>
<td>".$resultado['celular']."</td>
<td>".$resultado['direccion']."</td>
<td>".$resultado['correo']."</td>
<td>".$resultado['hora']."</td>
<td>".$retVal = ($resultado['atendido'] == 1) ? 'Si' : 'No'."</td>
<input type='hidden' id='atendido' name='atendido' value=".$val.">
<input type='hidden' id='idu' name='idu' value=".$resultado['idsolicitar_cita'].">
<td><button id='enviar' name='enviar' class='enviar'><img src='img/checkmark.png' width='15px' height='15px' ".$Val = ($resultado['atendido'] == 1) ? 'disabled' : 'enabled'."></button></td>
</tr>";
}
$tabla .= "</tbody>
</table>";
}
echo $tabla;
} catch (PDOException $e) {
echo 'Error: ' . $e->getMessage(). '<br/>';
}
}
?>
template_table.php
<?php
$atendido = $_POST['atendido'];
$idu = $_POST['idcita'];
try {
include_once("conexion.php");
$stmt = $con_bd->prepare('UPDATE solicitar_cita SET atendido = :atendido WHERE idsolicitar_cita = :idu');
$stmt->bindParam(':atendido', $atendido);
$stmt->bindParam(':idu', $idu);
$rows = $stmt->execute();
$com = '';
if ($rows) {
$com = 'completado';
echo $com;
}
} catch (Exception $e) {
echo 'Error: ' . $e->getMessage();
}
?>