When a user clicks on an icon, a certain emploee_id is transmitted to the server on the basis of which the server makes the calculations and if these calculations are correct, a pdf document should open in a new browser tab (which is currently happening).
But if validation fails, the user must be notified by a pop-up message describing the errors.
Here I can not figure out how to do it. The problem is that on the client responseType = 'blob' - if you change it, for example on json, the pdf document opens with errors.
Throughout the application, data is sent from the server in the form of json:
{"status_code": 200, "status": "success", "text": null, "data": [{...}, ...]}.
But the underlying code I can not fit under this format.
PS: I tried to convert the final output string to the server via base64_encode and pack everything into json of the form:
{ "status": "success", "binary_data": base64_encode($string) } and decode on the client through window.atob (string) and substitute it into the constructor new Blob (), but nothing comes out, the document opens with errors
Server code:
// Π·Π΄Π΅ΡΡ ΠΏΡΠΎΠΈΡΡ
ΠΎΠ΄ΠΈΡ Π½Π΅ΡΠΊΠΎΠ»ΡΠΊΠΎ ΡΡΠ°ΠΏΠΎΠ² Π²Π°Π»ΠΈΠ΄Π°ΡΠΈΠΈ ΠΏΠΎ ΡΠ°Π·Π»ΠΈΡΠ½ΡΠΌ ΠΏΡΠ°Π²ΠΈΠ»Π°ΠΌ // Π² ΡΠ΅Π·ΡΠ»ΡΡΠ°ΡΠ΅ ΡΠ΅Π³ΠΎ Π² $errors ΡΠΎΡΠΌΠΈΡΡΠ΅ΡΡΡ ΡΡΡΠΎΠΊΠ° ΠΈΠ· ΠΎΡΠΈΠ±ΠΎΠΊ ... if(!$this->form_validation->run('mto_rules')) { $errors .= validation_errors(); } if($errors !== '') { // Π·Π΄Π΅ΡΡ Π½ΡΠΆΠ½ΠΎ ΠΊΠ°ΠΊ-ΡΠΎ ΡΠ²Π΅Π΄ΠΎΠΌΠΈΡΡ ΠΏΠΎΠ»ΡΠ·ΠΎΠ²Π°ΡΠ΅Π»Ρ ΠΎΠ± ΠΎΡΠΈΠ±ΠΊΠ°Ρ
// Π²ΠΎ Π²ΡΠ΅ΠΌ ΠΏΡΠΈΠ»ΠΎΠΆΠ΅Π½ΠΈΠΈ ΡΠ΅Π°Π»ΠΈΠ·ΠΎΠ²Π°Π½ΠΎ: // $response['status_code'] = 200; // $response['status'] = 'error'; // $response['text'] = $errors; // .... // ΡΡΡ ΠΌΠΎΠ³ΡΡ Π±ΡΡΡ Π²ΡΡΠΈΡΠ»Π΅Π½ΠΈΡ ΠΊΠ°ΠΊΠΈΠ΅-Π»ΠΈΠ±ΠΎ // .... // ΠΈ Π² ΡΠ°ΠΌΠΎΠΌ ΠΊΠΎΠ½ΡΠ΅ ΠΌΠ΅ΡΠΎΠ΄Π°, ΠΏΠΎΡΠ»Π΅ Π²ΡΠ΅Ρ
ΠΏΡΠΎΠ²Π΅ΡΠΎΠΊ // $this->output->set_status_header($response['status_code']) // ->set_output($response); } // Π€ΠΎΡΠΌΠΈΡΡΠ΅ΠΌ Π½Π΅ΠΊΠΈΠΉ html ΠΈ ΠΏΠ΅ΡΠ΅Π²ΠΎΠ΄ΠΈΠΌ Π² pdf Π±ΠΈΠ±Π»ΠΈΠΎΡΠ΅ΠΊΠΎΠΉ TCPDF ... ... $pdf->writeHTML($html, true, false, false, false, ''); // ΠΎΡΠ΄Π°Π΅ΠΌ Π±ΠΈΠ½Π°ΡΠ½ΡΡ ΡΡΡΠΎΠΊΡ - ΠΠΠΠΠ!!! echo $pdf->Output('', 'S'); Client code:
var xhr = new XMLHttpRequest() xhr.open('GET', 'my.php?emploee_id=' + emploee_id, true) xhr.responseType = 'blob' xhr.onload = function(e) { var blob = new Blob([this.response],{type: 'application/pdf'}) var url = window.URL.createObjectURL(blob) window.open(url) } xhr.send()