0

I have 3 php files: request.php, action.php,mail.php.

request.php generates the page, filling it with different data depending on the POST data sent by ajax. It starts as a form, with multiple fields. When you click the submit button, ajax sends the form data to action.php via POST. action.php then processes the data, storing it in the database, and generating an html div that cleanly lays out the data. action.php then uses html2canvas and jsPDF to to the html div into a pdf, which then is passed to mail.php via jquery POST to be emailed out with phpmailer. action.php then generates a success message which is sent to request.php and displayed.

However, the javascript inside action.php that sends the pdf to mail.php always fails to run. I've tried a variety of ways to do it, but I can't seem to figure out another way to pass all the data where it is needed, as the data needs to be passed twice: first as an array of raw data, then as a pdf generated by html2canvas and jsPDF.

If I place a hidden iframe of mail.php inside action.php it will run fine and send the email, but it will be an empty pdf not filled with any of the data from request.php (obviously).

I can think of a few potential solutions but I cannot figure out how to implement them:

1) It seems the issue is that javascript doesn't like to run nested, but perhaps there is a way to force the javascript code inside action.php to run so that the pdf is generated and sent to mail.php.

2) Maybe there is a way to do it all with only one data pass that I am not seeing.

3) Maybe there is a way while inside action.php to pass the data array to an iframe for process.php which would format the html div and generate the pdf and then send it off to mail.php, but I assume even if I could get that done I would run into the same problem of the javascript in process.php failing to run and send the pdf to mail.php.

The_DemoCorgin
  • 744
  • 6
  • 19

2 Answers2

1

You have a fairly normal (for starting out) misunderstanding of how JavaScript and PHP work together. PHP runs on your server. JavaScript runs on your users' browsers. Based on what you have above, what you are trying to do is:

  1. load the initial page via request.php
  2. process a data request with action.php and send a formatted table to the JS
  3. have the JS send that table to mail.php

What you should be doing instead:

  1. load the initial page via request.php
  2. process a data request with action.php
    • have action.php call mail.php, giving it the table
    • have action.php send the table back to the JS
  3. have mail.php email out the table

You ask why the JavaScript never gets run to send the data to the third script, and that's because you likely never call it. More importantly though, as I show above, you don't need it: just have action.php call mail.php.

Matthew Herbst
  • 29,477
  • 23
  • 85
  • 128
  • Added code. I feel like I do have `action.php` call `mail.php`. Maybe I'm misunderstanding what you're saying. Maybe my question will be more clear with example code. – The_DemoCorgin Feb 05 '16 at 00:28
  • You are not having `action.php` call `mail.php`. You are having the page rendered by `response.php` (I'm assuming your `index.php` page) send data it is getting from your server (`action.php`) back to your server (`mail.php`). [This question](http://stackoverflow.com/questions/8450696/execute-a-php-script-from-another-php-script) has an excellent answer on how to call PHP scripts from other PHP scripts. Think of it this way: in your current model, I ask for the table, you give it to me, then you ask me to give it back to you so you can email it out. Cut out the middle man. – Matthew Herbst Feb 05 '16 at 00:49
  • I think I am understanding some of what you are saying. But I believe I still need to call JS at some point to generate the pdf. Right now my current model is: Server gives you a form, you fill it out and send it back to the server. Server shows you the results of that form, then the server needs to use JS to essentially take a picture of the form. Then that picture is sent to the server so it can email it out. Even if I have `action.php` call `mail.php`, I still need to run JS at some point before that to generate the PDF so `action.php` has something to pass to `mail.php` – The_DemoCorgin Feb 05 '16 at 01:09
  • @tsHunter take a look at [this question](http://stackoverflow.com/questions/8163963/generate-pdf-from-html-php). It (and other questions like it) shows some ways of doing the PFD generation directly in PHP. – Matthew Herbst Feb 05 '16 at 01:46
0

Maybe not the answer I was looking for, but since I knew I didn't want to use php pdf creation as Matthew suggested, this ended up working for me.

Since request.php had the javascript that worked with action.php anyways, I ended up sending the html table from action.php back to request.php, and then having request.php call mail.php.

The_DemoCorgin
  • 744
  • 6
  • 19