Consultation in ajax

0

$(document).ready(function () {
    var click = $('.click');
    click.on('click', function () {
        var id = $(this).data('id');
        usuario = '<?php $idMesero;?>';
        for (var i = 1; i <= 14; i++) {
            switch (id) {
                case i:
                    //$('#' + id).addClass(presionado);
                    //time(id);
                    var request = $.ajax({
                        method: 'GET',
                        url: '../php/funciones.php', //el archivo server .php al que quieres enviar                    
                        data: {
                            dato: id,
                            user: usuario //dato que quieres enviar
                        },
                        dataType: 'json' //el tipo de dato tambien puede ser text
                    });

                    request.done(function (response) {
                        if (response.status) {
                            alert('Success: ' + response.result + "\n Message: " + response.message);
                            //window.location.href = '../php/ordena.php';
                        } else {
                            alert('Error: ' + response.message);
                            //window.location.href = window.location.href;
                        }
                    });

                    request.fail(function (jqXHR, textStatus) {
                        alert("Server request failed: " + textStatus);
                        //window.location.href = '';
                    });
                    break;
            }
        }
    });
});
<?php
    require 'conexion.php';
    #require 'login.php';
    require 'include/datosHamburguesas.php';

if(isset($_GET) && isset($_GET['dato'] && isset($_GET['usuario'])){
    header('Content-Type: application/json');

    $identificador = $_GET['dato'];
    $usuario = $_GET['usuario'];
    if($identificador == 1){
        $inserta = "INSERT INTO $comanda (idComanda, idEmpleado, idProductos, fechaCom, noMesa, cantidadProd) 
                                VALUES (3, $usuario, $idH, CURDATE(), 20, 1)";
        $con->query($inserta);
        echo json_encode(array(//pruebas si funciona imprimiendolo
            'status' => 1,
            'result' => $identificador,
            'message' => "listo"
        ), true);
    }
}
?>

I want to get a record of a table with ajax and then pass it to another php file. Osea in a table I have the id of a user and, I want to recover with ajax to be able to use it in an insert, in another php file.

    
asked by Acnologia 21.05.2018 в 00:57
source

2 answers

1

What do you think of a sessión variable?

Suppose this is your first code where you retrieve the user ID

<?php 
$query = "SELECT ID FROM EMPLEADOS WHERE NOMBRE = 'FOO'";
if( $resultado = mysqli_query($con,$query)){

  if( mysqli_num_rows( $resultado ) > 0){
    $fila = mysqli_fetch_array( $resultado );
    //Aquí hacemos la variable de sesión, iniciamos sesión y creamos una nueva variable
    session_start();
    $_SESSION['id'] = $fila['ID'];
  }
  mysqli_free_result( $resultado );
  mysqli_close( $con );
}
?>

Now your INSERT would be like this

<?php
    //Inicias tu sesión
    session_start();
    require 'conexion.php';
    require 'include/datosHamburguesas.php';

if(isset($_GET) && isset($_GET['dato'] && isset($_GET['usuario'])){
    header('Content-Type: application/json');

    $identificador = $_GET['dato'];

    //Ahora en lugar de enviar el ID, recuperamos con la sessión del servidor
    $usuario = $_SESSION['id'];
    if($identificador == 1){
        $inserta = "INSERT INTO $comanda (idComanda, idEmpleado, idProductos, fechaCom, noMesa, cantidadProd) 
                                VALUES (3, $usuario, $idH, CURDATE(), 20, 1)";
        $con->query($inserta);
        echo json_encode(array(//pruebas si funciona imprimiendolo
            'status' => 1,
            'result' => $identificador,
            'message' => "listo"
        ), true);
    }
}
?>

I think it would be more practical to do it that way and you do not fight with FRONT anymore

EDITO

I already saw the error, you are trying to recover a variable that does not exist

//El índice usuario en el GET no existe, debería ser user
$identificador = $_GET['dato'];
$usuario = $_GET['user'];

data: {
 dato: id,
 user: usuario //dato que quieres enviar
},

You can check in your PHP if you do var_dump($GET);

    
answered by 21.05.2018 / 02:24
source
1

I tell you that you can do it with the post function, in the following example you pass the values to "example_ajax_process.php"

<html>
 
<head>
 
<title>Ejemplo sencillo de AJAX</title>
 
<script type="text/javascript" src="/js/jquery.js"></script>
 
<script>
function realizaProceso(valorCaja1, valorCaja2){
        var parametros = {
                "valorCaja1" : valorCaja1,
                "valorCaja2" : valorCaja2
        };
        $.ajax({
                data:  parametros,
                url:   'ejemplo_ajax_proceso.php',
                type:  'post',
                beforeSend: function () {
                        $("#resultado").html("Procesando, espere por favor...");
                },
                success:  function (response) {
                        $("#resultado").html(response);
                }
        });
}
</script>
 
</head>
 
<body>
 
    
answered by 21.05.2018 в 01:10