me gustaria ayuda con algo, necesito alamacenar el valor de un input puesto en datatables, obtengo valores de mi tabla y los tomo de manera buena, pero cuando agregue un input y quise tomar su valor no me funciono y el array arroja un String(0). me gustaria saber como puedo tomar todos los valores incluyendo el que inserto en el input.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<?php include 'header.php' ?>
<body>
<div class="container shadow p-3 mt-5 bg-body rounded" style="background:#fff;">
<table class="table table-striped table-bordered" data-page-length="2" id="tbl-buys">
<thead>
<tr>
<th>
<input type="checkbox" />
</th>
<th>Id</th>
<th>Nombre</th>
<th>Descripcion</th>
<th>Cantidad</th>
</tr>
</thead>
<tbody></tbody>
</table>
<br>
<button id="btnObtener">Obtener</button>
</div>
</body>
<?php include 'footer.php' ?>
<script>
$(document).ready(function() {
let $dt = $('#tbl-buys');
let dt = $dt.DataTable({
ajax: {
url: "src/sql_consult/crud_crop.php",
type: "post",
dataType: "json",
data: {
option: 4
},
dataSrc: "",
},
order: false,
language: {
url: "src/datatables/spanish.json"
},
columns: [{
render: function(data, type, full, meta) {
// ACA controlamos la propiedad para des/marcar el input
return "<input type='checkbox'" + (full.checked ? ' checked' : '') + "/>";
},
orderable: false
},
{
data: 'idCultivo',
orderable: false
},
{
data: 'nombre',
orderable: false
},
{
data: 'descripcion',
orderable: false
},
{
data: null,
render: function(data) {
return "<input type='text' class='form-control w-25' id='cant'>";
},
},
]
});
let $total = $('#total');
// Cuando hacen click en el checkbox del thead
$dt.on('change', 'thead input', function(evt) {
let checked = this.checked;
let total = 0;
let data = [];
dt.data().each(function(info) {
// ACA cambiamos el valor de la propiedad
info.checked = checked;
// ACA accedemos a las propiedades del objeto
if (info.checked) total += info.Precio;
data.push(info);
});
dt.clear()
.rows.add(data)
.draw();
$total.val(total);
});
// Cuando hacen click en los checkbox del tbody
$dt.on('change', 'tbody input', function() {
let info = dt.row($(this).closest('tr')).data();
let total = parseFloat($total.val());
// ACA accedemos a las propiedades del objeto
info.checked = this.checked;
let price = info.Precio;
total += info.checked ? price : price * -1;
$total.val(total);
});
// ACA suscribimos un listener
$('#btnObtener').on('click', function() {
let dt = $('#tbl-buys').DataTable();
let checkeds = dt.data().toArray().filter((data) => data.checked);
$.ajax({
url: 'src/sql_consult/prueba.php',
data: {
checkeds: checkeds
},
type: 'POST',
dataType: 'json',
success: function(data) {
}
})
});
});
crud_crop.php
<?php
include '../database/bd.php';
$id = (isset($_POST['id'])) ? $_POST['id'] : '';
$nombre = (isset($_POST['nombre']))?$_POST['nombre']:'';
$description = (isset($_POST['description']))?$_POST['description']:'';
$status = (isset($_POST['status']))?$_POST['status']:'';
$option = (isset($_POST['option']))?$_POST['option']:'';
switch ($option) {
case 1:
$query = mysqli_query($conexion,"SELECT * FROM cultivo WHERE nombre= '$nombre' OR descripcion = '$description' ");
if ($res = mysqli_fetch_array($query) > 0) {
$data = 0;
}else {
$query = mysqli_query($conexion,"INSERT INTO cultivo (idCultivo,nombre,descripcion,estado)
VALUES('$id','$nombre','$description','$status')");
$query = mysqli_query($conexion,"SELECT * FROM cultivo ORDER BY idCultivo DESC LIMIT 1");
while ($res = mysqli_fetch_assoc($query)) {
$data [] = $res;
}
}
break;
case 2:
$query = mysqli_query($conexion,"SELECT * FROM cultivo WHERE (nombre= '$nombre' AND idCultivo != '$id') OR (descripcion = '$description' AND idCultivo != '$id') ");
if ($res = mysqli_fetch_array($query) > 0) {
$data = 0;
}else {
$query = mysqli_query($conexion,"UPDATE cultivo SET nombre='$nombre',descripcion='$description'WHERE idCultivo = $id ");
$query = mysqli_query($conexion,"SELECT * FROM cultivo ORDER BY idCultivo DESC LIMIT 1");
while ($res = mysqli_fetch_assoc($query)) {
$data [] = $res;
}
}
break;
case 3:
$query = mysqli_query($conexion,"UPDATE cultivo SET estado= '0' WHERE idCultivo = $id");
$query = mysqli_query($conexion,"SELECT * FROM cultivo");
while ($res = mysqli_fetch_assoc($query)) {
$data [] = $res;
}
break;
case 4:
$query = mysqli_query($conexion,"SELECT * FROM cultivo WHERE estado = 1");
while ($res = mysqli_fetch_assoc($query)) {
$data [] = $res;
}
break;
case 5:
$query = mysqli_query($conexion,"SELECT * FROM cultivo WHERE estado = 0");
$data = array();
while ($res = mysqli_fetch_assoc($query)) {
if (!empty($res)) {
$data [] = $res;
}else {
$data['data'][] = null;
}
}
break;
case 6:
$query = mysqli_query($conexion,"UPDATE cultivo SET estado= '1' WHERE idCultivo = $id");
$query = mysqli_query($conexion,"SELECT * FROM cultivo");
while ($res = mysqli_fetch_assoc($query)) {
$data [] = $res;
}
break;
}
print json_encode($data);
mysqli_close($conexion);
?>
prueba.php
<?php
include '../database/bd.php';
$array = (isset($_POST['checkeds']))?$_POST['checkeds']:'';
var_dump($array);
?>
Bien, si se fijan envio checkeds, pues ahi se almacenaron todos los valores, pero como mencione anteriormente menos el del input, quite las lineas de codigo donde intente agregar el input porque no servian y no conozco mucho de este tipo de datatables que es con checkbox.
Inserto una imagen donde estoy haciendo "pruebas".
</head>
y el footer.php y el script antes del cierre del</body>
. Ya se que no tiene que ver con la respuesta que esperas, pero pinta que estan mal puestos y puedes obtener resultados imprevisibles según el navegador que los interprete seguramente.src/sql_consult/crud_crop.php