Reputation: 744
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
.
Upvotes: 0
Views: 311
Reputation: 744
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
.
Upvotes: 0
Reputation: 31973
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:
request.php
action.php
and send a formatted table to the JSmail.php
What you should be doing instead:
request.php
action.php
action.php
call mail.php
, giving it the tableaction.php
send the table back to the JSmail.php
email out the tableYou 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
.
Upvotes: 1