Я кодирую систему CRUD, используя PHP (с CodeIgniter) AJAX, Datatables и MySQL. Почти все работает нормально, но процесс обновления и удаления.
Вы можете обновить или удалить ОДНУ запись, но если вы хотите обновить / удалить две или более строки подряд, вы получите следующую ошибку в консоли javascript :
BlockquoteUncaught TypeError: Невозможно прочитать свойство 'id' из неопределенного
Вы можете удалить / обновить другую запись после перезагрузки всей страницы.
Это мой html код:
<form role="form" id="agregarStaffFrm" class="needs-validation" novalidate>
<div class="form-group">
<div class="form-label">
<label for="usuario">Usuario</label>
<input type="text" id="c_usuario" name="c_usuario" class="form-control" placeholder="Usuario"
required="required" autofocus="autofocus">
<div class="valid-feedback">
Looks good!
</div>
</div>
</div>
<div class="form-group">
<div class="form-label">
<label for="nombre">Nombre</label>
<input type="text" id="c_nombre" name="c_nombre" class="form-control" placeholder="Nombre"
required="required" autofocus="autofocus">
</div>
</div>
<div class="form-group">
<div class="form-label">
<label for="apellido">Apellido</label>
<input type="text" id="c_apellido" name="c_apellido" class="form-control" placeholder="Apellido"
autofocus="autofocus">
</div>
</div>
<div class="form-group">
<div class="form-label">
<label for="password">Password</label>
<input type="password" id="c_password" name="c_password" class="form-control" placeholder="Password"
autofocus="autofocus">
</div>
</div>
<div class="form-group">
<label for="categoria">Sucursal</label>
<select class="form-control" id="c_sucursal" name="c_sucursal">
<?php
foreach ($sucursales as $suc)
{
echo '<option value="'.$suc['nombre'].'">'.$suc['nombre'].'</option>';
}
?>
</select>
</div>
<button type="button" class="agregar btn btn-success" id="agregar_Staff_btn">Aceptar</button>
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cerrar</button>
Это код javascript для получения данных из таблиц данных и заполнения формы обновления:
var editar_staff = function(tbody, table){
$(tbody).on("click", "button.editar", function(){
var data = table.row($(this).parents("tr")).data();
var id = $("#u_id").val(data.id);
var nombre = $("#nombre").val(data.Nombre);
var apellido = $("#apellido").val(data.Apellido);
});
}
Это код обновить (сохранить) данные после того, как пользователь их изменил:
var guardar = function (){
$("#staffFrm").on("submit", function(e){
e.preventDefault();
var frm = $(this).serialize();
$.ajax({
method: "POST",
url: "<?php echo site_url('control/u_cajero')?>",
data: frm
}).done(function(info){
var json_info = JSON.parse(info);
$("#staff").modal("hide");
limpiar_formulario();
document.getElementById("mensajeAlerta").innerHTML = json_info;
listar();
});
});
}
Любая помощь будет принята с благодарностью.