Query a database based on form input then output to another page [closed]

This is the way you could handle it, though opinions may vary. Getting the framework up will be a bit complicated and making it pretty is another task entirely.

I would make it sexy by running an AJAX query when you submit the form. The AJAX script collects all the data from the boxes on the page, makes the call to the back end, then renders visible a “Results:” box that was on the page the whole time, and inserts the resulting HTML into that box. That’s how slick search results work, basically.

The results box has a button to create a PDF. Java is watching that button and what that button has is a data-attribute holding, probably, the very same HTML that was in the results box. You have inserted that HTML simultaneously when you inserted it into the results box. So now Java can send that data-attr to the PDF writer without hitting the back end again. This will come out as the “clean screen” you asked for, but even better can be downloaded or emailed. 🙂

Here is an example java function that creates a form, appends an object to the form and then submits the form. You would submit this to your “PDF Writer” script. You populate the object (obj) submitted to this function with all the data you want to submit – probably just your html but the possibilities are endless.

function submitACP(obj){
        // form submission to ACP with current vars     
        var scheduleForm = document.createElement("form");
        scheduleForm.id = "transmission_form";
        scheduleForm.target = "_self";
        scheduleForm.method = "post"; // or "POST" if appropriate
        scheduleForm.enctype = "multipart/form-data";
        if (isset(obj.clientID) && obj.clientID > 0) {
            scheduleForm.action = 'https://'+$(location).attr('host')+'/create-pdf/'; 
        } else {
            scheduleForm.action = 'https://'+$(location).attr('host')+'/client-selector/'; // need a client id
        }
        var transmissionData = encodeURIComponent(JSON.stringify(obj));
        console.log('data stringified:'+transmissionData);

        var transmission_object = document.createElement("input");
        transmission_object.type = "text";
        transmission_object.name = "client_object";
        transmission_object.value = transmissionData;
        scheduleForm.appendChild(transmission_object);

        document.body.appendChild(scheduleForm);

        $('#transmission_form').submit();
    }

For a PDF writer I use TCPDF. It takes a lot of trial and error to get it right, but it’s solid. First thing you do is start output buffering to prevent anything ruining your PDF. Then you run your logic, capturing whatever you want into a string variable. Stop the buffering, then require the TCPDF directories and invoke it:

ob_start();
set_time_limit(30);

$path = $_SERVER['DOCUMENT_ROOT'];
//define( 'SHORTINIT', true );
require( $path.'/wp-load.php' );

$data_array = json_decode(stripslashes(urldecode($_POST['client_object'] )), true );

if (! wp_verify_nonce($data_array['nonce'], 'pdf_nonce')) {
    die('sorry, charlie.');
}
$doc_label = wp_kses($data_array['client_name']);
echo wp_kses($data_array['html']);

// Include the main TCPDF library (search for installation path).
require __DIR__ . '/vendor/tcpdf/config/tcpdf_config.php';
require __DIR__ . '/vendor/tcpdf/tcpdf.php';

// create new PDF document
ob_end_clean();
$pdf = new TCPDF(PDF_PAGE_ORIENTATION, PDF_UNIT, PDF_PAGE_FORMAT, true, 'UTF-8', false);

// set document information
$pdf->SetCreator(PDF_CREATOR);
$pdf->SetAuthor('Sales Robot');
$pdf->SetTitle($doc_label);
$pdf->SetSubject('Hardcopy Schedule');
//$pdf->SetKeywords('TCPDF, PDF, example, test, guide');

// set default header data
$pdf->SetHeaderData(PDF_HEADER_LOGO);

// set header and footer fonts
$pdf->setHeaderFont(Array(PDF_FONT_NAME_MAIN, '', PDF_FONT_SIZE_MAIN));
$pdf->setFooterFont(Array(PDF_FONT_NAME_DATA, '', PDF_FONT_SIZE_DATA));

// set default monospaced font
$pdf->SetDefaultMonospacedFont(PDF_FONT_MONOSPACED);

// set margins
$pdf->SetMargins(PDF_MARGIN_LEFT, PDF_MARGIN_TOP, PDF_MARGIN_RIGHT);
$pdf->SetHeaderMargin(PDF_MARGIN_HEADER);
$pdf->SetFooterMargin(PDF_MARGIN_FOOTER);

// set auto page breaks
$pdf->SetAutoPageBreak(TRUE, PDF_MARGIN_BOTTOM);

// set image scale factor
$pdf->setImageScale(PDF_IMAGE_SCALE_RATIO);

// set some language-dependent strings (optional)
if (@file_exists(dirname(__FILE__).'/lang/eng.php')) {
    require_once(dirname(__FILE__).'/lang/eng.php');
    $pdf->setLanguageArray($l);
}

// ---------------------------------------------------------

// set font
$pdf->SetFont('helvetica', 'B', 20);

// add a page
$pdf->AddPage();

//$pdf->Write(0, 'Example of HTML tables', '', 0, 'L', true, 0, false, false, 0);

$pdf->SetFont('helvetica', '', 12);

$pdf->writeHTML($hc_html, true, false, false, false, '');
$doc_label .= '.pdf';
//Close and output PDF document
$pdf->Output($doc_label, 'I');

//============================================================+
// END OF FILE
//============================================================+

die();
?>

So like I said, you can come at this from a number of directions. If you don’t have a handle on java and AJAX you can just submit a form to your PDF writer page, which, after starting the output buffer!, runs the logic on your form variables, outputs to a variable and then uses the variable in the invocation of the PDF writer.