Record and duplicate a record

0

I currently have a button to record and continue recording another record

<button type="submit" name="input" id="input">Grabar y Agregar</button>

and he does it well, I want to put another two

<button type="submit" name="input" id="input>Grabar y Duplicar</button>

and

<button type="submit" name="input" id="input" >Grabar y Salir</button>

How do I record and duplicate the values of the current record and be ready to record? and how do I record and redirect to another page?

code where the record is saved ...

        // check for duplicate code
    $stmt = $conn->prepare("SELECT * FROM ".$database_table_prefix."products WHERE prd_code = ?");
    $stmt->execute([$code]);
    $exist_code = $stmt->fetchColumn();

    if($exist_code!=0)
    {
        echo '<div class="alert alert-danger background-danger" role="alert">
        <strong>Codigo de Producto ya Existe!</strong> 
        <button type="button" class="close" data-dismiss="alert" aria-label="Close">
        <span aria-hidden="true">&times;</span>
        </button>
        </div>';

    exit();
    }

    //insert
    $insert = $conn->prepare("INSERT INTO ".$database_table_prefix."products(prd_id, prd_code, prd_name, prd_description, prd_ref, prd_brand, prd_ubic, prd_cat, prd_img, prd_pricesell1, prd_pricesell2, prd_pricesell3, prd_pricesell4, 
    prd_pricecost, prd_InvMinimo, prd_InvMaximo, prd_Existencias, prd_PorcentajeRF, prd_UnidadEmpaque, prd_Activo, prd_FechaCreacion, prd_PorcentajeIva, prd_UtilidadPrecio1, prd_UtilidadPrecio2, 
    prd_UtilidadPrecio3, prd_UtilidadPrecio4,   prd_ComisionXVtas, prd_Procedimiento, prd_Convertir, prd_IdProductoEquivalente, prd_CantidadEquivalente, prd_Inventario, prd_ColorBoton, 
    prd_ColorTexto, prd_UbicacionBoton, prd_Impresora1, prd_Impresora2, prd_Impresora3, prd_Impresora4, prd_CantidadEnPesa, prd_BotonEnFactura, prd_id_branch, prd_Estampilla, prd_GradosAlcohol, prd_TCantXUnidadEmpaque, prd_AdValorem, prd_PrecioPromDane, prd_ComponentesEnEntrada) 
    VALUES(NULL,'$code','$name','$descrip','$ref','$brand','$ubic','$Group','$img','$pv1','$pv2','$pv3','$pv4','$pcto','$invmin','$invmax','$stock','$rtefte','$pack','$active','$dateadd','$tax',
    '$utlpv1','$utlpv2','$utlpv3','$utlpv4','$comvtas','$porced','$convert','$proequival','$quantity', '$inventory','$btnclr','$btntxtclr','$btnubic','$prtr1','$prtr2','$prtr3','$prtr4','$weight',
    '$btninvoice','$branch','$liqStamp','$liquorsDegrees','$liquorsPack','$liquorsAdVal','$liquorsDane','$descargarEnEntrada')"); 

    $insert->execute();

    $prd_id = $conn->lastInsertId(); // last inserted ID
    
asked by Juan Carlos 07.02.2018 в 18:51
source

1 answer

0

You could execute the sending of your form through jquery so that it adds or not a control field that allows you to validate the action to execute at the time of sending, something like this:

Your buttons would change from submit to button

<button type="button" class="boton" data-tipo="agrega" name="input" id="input">Grabar y Agregar</button>
<button type="button" class="boton" data-tipo="duplica" name="input" id="input">Grabar y Duplicar</button>
<button type="button" class="boton" data-tipo="salir" name="input" id="input">Grabar y Salir</button>

An script that monitors the click on these and acts on an attribute

$(".boton").click(function(){
    //Tomamos el data atributo del boton pulsado
    var tipo = $(this).attr('data-tipo');

    switch (tipo) {
        case 'agrega':
            //Agregamos el Campo de Control
            $(this).append('<input type="hidden" name="tipo" value="duplicar"/>');
            //Enviamos el Formulario
            $("#formulario").submit();
        break;

        case 'agrega':
            $("#formulario").submit();
        break;
        case 'salir':
            $(this).append('<input type="hidden" name="tipo" value="salir"/>');
            //Enviamos el Formulario
            $("#formulario").submit();
        break;

    }
})

In your php file you would check this condition at the end of the execution of all your code, for example:

if(isset($_POST['tipo']) && $_POST["tipo"] == "duplicar"){
    $_SESSION["tmp_data"] = $_POST;//Creamos la variable temporal con la data enviada

}else if(isset($_POST['tipo']) && $_POST["tipo"] == "salir"){

    usset($_SESSION['tmp_data']);//eliminamos la variable en caso que exista
    header('location:tuarchivo.php');//Redirigimos a la pagina de salida

}else{
    usset($_SESSION['tmp_data']);//eliminamos la variable en caso que exista
}

Obviously your text fields would change to be able to load the data of the temporary session variable if it exists and only in the cases of duplicating code, example:

<input type="text" name="loquesea" value="<?php echo (isset($_SESSION["tmp_data"])) ? $_SESSION['tmp_data']["valorcampo"] : ""; ?>" />

Clearly I imagine there is some other way of doing what you are looking for, this is the one that occurs to me at this moment. Greetings

    
answered by 08.02.2018 в 01:23