dshgdhdfcas30210 2016-02-04 23:54
浏览 67

通过javascript通过多个php文件POST数据

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.

  • 写回答

2条回答

  • doukuiqian9911 2016-02-05 00:17
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!