TCPDF - Editable Fields - Javascript copy field to another one on update

16 Views Asked by At

Once a tcpdf is generated. We need to be able to copy the content of one field to another one. Thoses fields are editable.

Example of the code :

`    $formFieldLabels = array(
        'Name:' => 'name',
        'Address:' => 'address'
    );

    // Loop through form field labels and set values from form submission
    foreach ($formFieldLabels as $label => $fieldName) {
        if (isset($_POST[$fieldName])) {
            $fieldValue = $_POST[$fieldName];
            if (is_array($fieldValue)) {
                foreach ($fieldValue as $key => $value) {
                    $html = '<div><p>1'.$fieldName.'-' . $label . '</p></div><div>';
                    $pdf->TextField($fieldName . '[' . $key . ']', 50, 10 + ($key * 20), array('value' => $value));
                    $html .= '</div>';
                    $pdf->writeHTML($html, true, false, true, false, '');
                }
            } else {
                $html = '<div><p>2'.$fieldName.'-' . $label . '</p></div><div>';
                $pdf->TextField($fieldName, 50, 10, array('value' => $fieldValue));
                $html .= '</div>';
                $pdf->writeHTML($html, true, false, true, false, '');
            }
        }
    }


//// PAGE 2 Content
$pdf->AddPage();
$html = '<div><h2>PAGE 2 Content</h2></div>';
 $pdf->writeHTML($html, true, false, true, false, '');

//// PAGE 3 Content
$pdf->AddPage();
$html = '<div><h2>PAGE 6 Content</h2></div>';

/*content*/
$html = '<div><p>name 2'.$_POST['name'].'</p></div><div>';
                $pdf->TextField('name', 50, 10, array('value' => $_POST['name'])); 
                $html .= '</div>';
$pdf->writeHTML($html, true, false, true, false, '');


// JavaScript example from tcpdf with 'name'
$js = <<<EOD
var nomClientValue = 'test';
app.alert('JavaScript Popup Example for ' + nomClientValue, 3, 0, 'Welcome');
var cResponse = app.response({
    cQuestion: 'How are you today?',
    cTitle: 'Your Health Status',
    cDefault: 'Fine',
    cLabel: 'Response:'
});
if (cResponse == null) {
    app.alert('Thanks for trying anyway.', 3, 0, 'Result');
} else {
    app.alert('You responded, "'+cResponse+'", to the health question.', 3, 0, 'Result');
}
EOD;

// force print dialog
$js .= 'print(true);';

// set javascript
$pdf->IncludeJS($js);`

As you can see the first part of the code generate the fields ... The Javasciprt id the example from tcpdf for javascript. That work once the pdf is saved locally.

But we are trying to modify the data from the first editable field to second field when you edit it locally.

Any thought's on how I could do that ?

My guess would be to retreive the value from one maybe via an id or something like that

0

There are 0 best solutions below