I tell you how I have my structure:
I have a php called create_boxes.php . In this I open a box to be able to later open a point of sale (a table is created with the open boxes) then when opening the point of sale, which is in another php file (called punto_de_venta.php ). And that point of sale shows me the products of the branch where the box was opened, but I pass this variable through the url and receive it with GET in point_of_sale.php using:
$item = $_GET['sucursal'] ;
but this is where my problem comes from. I modified the way in which the html tables are created with the information from the database . I do it through the Jquery Datatable plugin so I no longer have the body of my table in the html , now I do it with ajax (it looks like this):
var sucursal = $('#valorSucursal').val();
$('.Tabla_Productos').DataTable({
"ajax": "ajax/datatable-productos.ajax.php",
"deferRender": true,
"retrieve": true,
"processing": true,
"data": {
"sucursal": sucursal
},
"language": {
"sProcessing": "Procesando...",
"sLengthMenu": "Mostrar _MENU_ registros",
"sZeroRecords": "No se encontraron resultados",
"sEmptyTable": "Ningún dato disponible en esta tabla",
"sInfo": "Mostrando registros del _START_ al _END_ de un total de _TOTAL_",
"sInfoEmpty": "Mostrando registros del 0 al 0 de un total de 0",
"sInfoFiltered": "(filtrado de un total de _MAX_ registros)",
"sInfoPostFix": "",
"sSearch": "Buscar:",
"sUrl": "",
"sInfoThousands": ",",
"sLoadingRecords": "Cargando...",
"oPaginate": {
"sFirst": "Primero",
"sLast": "Último",
"sNext": "Siguiente",
"sPrevious": "Anterior"
},
"oAria": {
"sSortAscending": ": Activar para ordenar la columna de manera ascendente",
"sSortDescending": ": Activar para ordenar la columna de manera descendente"
}
}
});
My question is how can I pass the variable
$item = $_GET['sucursal'] ;
by my ajax to my other php ( datatable-products.ajax.php ) to be able to work the SQL statement there :
$stmt = Conexion::conectar()->prepare("SELECT * FROM $tabla WHERE sucursal = '$item' ");
since this php is not receiving the variable that is passed to the point_of_sale.php .
It's also worth mentioning that I'm using MVC for my php .
Change what you have put in your ajax to this:
And then, in datatable-products.ajax.php (whose code appears in this other question: How to use this PHP variable inside another PHP ) you add this before calling the function
mostarTablaProductos()
:and then, when calling it, you do it by passing the parameter
$item
, that is:And finally, change the function definition by adding the new parameter to it, like this:
And I would say that it is. If I have not made a mistake, which could also be, with that you have managed to send the value of
$item
correctly between different php files as you asked in your question.Explanation:
We have correctly put the syntax to the
ajax
datatables call, because before you had put this:but in reality the syntax, as documented on its website , was this:
With that we get that, just like jquery , as the datatables documentation itself says , we pass the variable through POST
sucursal
:which later, looking at the rest of the code added in this answer, we collect with POST and apply it to the function
mostarTablaProductos
inside the datatable-products.ajax.php file .I trust that it works, you will tell us how it is.