Obtain the ID of an Order, at the moment of sending it by PHP to BBDD (For a CSV)

2

I try to do the following:

  • When you send a FORM , you generate an order, which, through Ajax, you send to the database.

  • Next, I have another Ajax, (both in jQuery when submitting) that pulls out a csv file from the order:

    <?php
    $cod_cliente = ($_POST['cod_cliente']);
    $optica = ($_POST['optica']);
    $fecha = ($_POST['fecha']);
    $tipo = ($_POST['tipo']);
    
    
    $csv = "../pedidos/PEDIDO ".$tipo.".csv";
    
    $lista = array (
        array('Codigo Cliente', 'Cliente', 'Fecha', 'Tipo'),
        array($cod_cliente, $optica, $fecha, $tipo),
    );
    
    $fp = fopen($csv, 'x');
    
    foreach ($lista as $campos) {
        fputcsv($fp, $campos);
    }
    
    fclose($fp);
    

    ? >

The complete ajax : submit:

$("#pedidos").submit(function(e){

  // Aquí definidas todas las variables

  e.preventDefault();

  $.ajax({
    type: 'POST',
    url: 'php/enviar_pedido.php',
    data: {'cod_cliente': cod_cliente,
            ...etc
            }
  })
  .done(function(listas_rep){
  })
  .fail(function(){
  })

  $.ajax({
    type: 'POST',
    url: 'php/generarCsv.php',
    data: {'cod_cliente': cod_cliente,
           'optica': optica,
           'fecha': fecha,
           'tipo': tipoCi}
  })
  .done(function(listas_rep){
    alert('CSV Generado con exito');
  })
  .fail(function(){
    alert('Error al crear el archivo CSV')
  })
})

My question is this: this request is sent in turn that generates the csv file.

If you notice, the name of the csv file is: ORDER "type", even though what I need is that you get the format of: "ORDER 01" "ORDER 02"

  • The ID of the order is supposed to be a AUTOINCREMENT in the database, so there is the "ID" but the CSV I generate it at the moment I create the order, obviously, in that moment is not yet the ID.

Can someone please offer me some advice / solution?

    
asked by Javier Avila Fernandez 30.08.2018 в 16:01
source

1 answer

1

In your file that you keep in the BD, put the following.

header('Content-Type: application/json');
echo json_encode(['id' => $tu_id]);

This will return the id, in the data. Since the ajax calls are asynchronous, you must concatenate them in the done

$.ajax({
    type: 'POST',
    url: 'php/enviar_pedido.php',
    data: {'cod_cliente': cod_cliente,
            ...etc
            }
})
.done(function(data){
     $.ajax({
        type: 'POST',
        url: 'php/generarCsv.php',
        data: {'cod_cliente': cod_cliente,
           'optica': optica,
           'fecha': fecha,
           'tipo': tipoCi
           'id_de_la_bd' : data.id // aquí lo pasas
        }
    })
    .done(function(listas_rep){
       alert('CSV Generado con exito');
    })
     .fail(function(){
        alert('Error al crear el archivo CSV')
     });
})
.fail(function(){
})

Just concatenate the name of the form that best suits you in your php file

    
answered by 30.08.2018 / 17:32
source