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.

    评论

报告相同问题?

悬赏问题

  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥30 用arduino开发esp32控制ps2手柄一直报错
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 求chat4.0解答一道线性规划题,用lingo编程运行,第一问要求写出数学模型和lingo语言编程模型,第二问第三问解答就行,我的ddl要到了谁来求了
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题
  • ¥15 Visual Studio问题