Function export to Excel with PHP parameters

2

I want to adapt this function that I use to export the complete DB to Excel to a function that by passing a parameter (paq) I returned an excel with the matches of the DB.

My HTML of the button.

<button onclick="Export()" class="btn btn-primary">Exportar a Excel</button>

Export function in JS.

function Export()
        {

            var conf = confirm("Exportar a CSV?");
            if(conf == true)
            {
                window.open("pasajeros/export.php", '_blank');
            }
        }

And the export.php

<?php

require("ajax/db_connection2.php");

$query = "SELECT * FROM users";
if (!$result = mysqli_query($con, $query)) {
    exit(mysqli_error($con));
}

$users = array();
if (mysqli_num_rows($result) > 0) {
    while ($row = mysqli_fetch_assoc($result)) {
        $users[] = $row;
    }
}

header('Content-Type: text/csv; charset=utf-8');
header('Content-Disposition: attachment; filename=EMT_Pasajeros.csv');
$output = fopen('php://output', 'w');
fputcsv($output, array('ID', 'Nombre', 'Apellido', 'Email', 'F.Nacimiento', 'Telefono', 'Tel.Seguro', 'Paquete','$ VENTA', '$ EMISIÓN', '$ NETA', 'DNI', 'Vendedor', 'Link Comprobante'));

if (count($users) > 0) {
    foreach ($users as $row) {
        fputcsv($output, $row);
    }
}
?>

I would like to know how to link the parameter of the Export ('parameter') of the onlick, with the function of JS and that this parameter is passed to the passengers / export.php so that it makes the query to the DB with the WHERE parameter.

    
asked by Santiago 27.07.2017 в 20:04
source

1 answer

1

You have two options. You can use the answer method GET (minimum of changes in your code, but less secure) or the answer method POST (more changes to the code, but more secure) to achieve your goal. First, to receive this parameter in the JS function (from wherever you get it) and send it with these methods, you would do something like this:

GET

function Export(parametro)
{
    var conf = confirm("Exportar a CSV?");
    if(conf == true)
    {
        window.open("pasajeros/export.php?parametro="+parametro, '_blank');
    }
}

POST

function Export(parametro)
{
    var conf = confirm("Exportar a CSV?");
    if(conf == true)
    {
        var mdr = new XMLHttpRequest();
        mdr.open("POST", "pasajeros/export.php", true);
        mdr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8");
        mdr.send("parametro="+parametro);
    }
}

To get this parameter from the PHP side

GET

$query = "SELECT * FROM users WHERE id = ".$GET["parametro"];

POST

$query = "SELECT * FROM users WHERE id = ".$POST["parametro"];

As you can see, in the part of JavaScript we include the parameter in url that is required, and in the% share PHP we access the parameter thanks to the super global variable GET / POST , which is an arrangement.

Post Reply

  • You should always be wary of what any of your users enter I do not know where you will get that parameter from which we are speaking, but now more than ever it is important to parameterize all your dynamic queries and thus avoid SQL injection. For which I recommend you read this other question that talks about it: How to avoid SQL injection?
answered by 27.07.2017 / 20:26
source