简体   繁体   English

使用AJAX发布表单数据,并通过一次提交触发PHPExcel操作

[英]Post form data with AJAX and trigger PHPExcel action with one submit

I have a form where the submit posts data via AJAX and then refreshes a div without reloading the complete page. 我有一个表单,其中提交通过AJAX发布数据,然后刷新div而不重新加载整个页面。 So far ok and it works perfectly. 到目前为止还可以,并且运行良好。

My problem: I need to add a second action to this form. 我的问题:我需要在此表单中添加第二个操作。 Same values but different action. 相同的值,但动作不同。 Both actions work perfectly alone, but I just can't get them working within the same submit. 两种动作单独都能完美地工作,但是我无法让它们在同一提交中正常工作。

The second action is a script generating an xls with PHPExcel and triggering a download. 第二步是使用PHPExcel生成xls并触发下载的脚本。

How do I include the following process_xls.php into the AJAX so it's triggered with the same submit? 如何将以下process_xls.php包含在AJAX中,以便使用相同的提交触发它?

The process_xls.php process_xls.php

$year = $_POST['year'];
$nmbr = $_POST['nmbr'];
$code = $_POST['code'];

include("excel/PHPExcel.php");

$objPHPExcel = new PHPExcel();

// ....
// code creating xls here
// ....

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Summary.xlsx"');
header('Cache-Control: max-age=0');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');
$objWriter->save('php://output');
exit;

The HTML: HTML:

<div id="container">
 <form id="form_year" method="post">
 <input type="hidden" name="year" value="2015">
 <input type="hidden" name="nmbr" value="22">
 <input type="hidden" name="code" value="5Tvfr5T">
 <div class="button-small"><input type="submit" name="" value=""></div>
 </form>

 // ....
 // some data displayed here
 // ....

</div>

(the class " button-small " is only for CSS) (“ button-small ”类仅适用于CSS)

The Javascript Javascript

<script type="text/javascript">
$(document).ready(function()
{
 $(document).on('submit', '#form_year', function()
 {      
  var data = $(this).serialize(); 

  $.ajax({  
  type : 'POST',
  url  : 'process_year_data.php',
  data : data,
  success :  function(data)
   {
    $("#form_year").fadeOut(500).hide(function()
    {
     $("#container").fadeIn(500).show(function()
     {
     $("#container").html(data);
     });
    });
   }
  });
  return false;
 });


});
</script>

Any help greatly appreciated ! 任何帮助,不胜感激!

I think to start with it would be better practice to target just that single form rather than looking into the document and targeting that again. 我认为一开始最好只针对单一表单,而不要研究文档并再次针对它。

By moving the return false into an if statement we ensure that the default method of the form isn't prevented. 通过将return false移到if语句中,我们确保不阻止表单的默认方法。 There is a bit of reading about event preventing here 有一点阅读有关事件预防这里

$(document).ready(function(){
    var posted = false;
    $('#form_year').submit(function(){
        if (!posted) {
            var data = $(this).serialize();
            $.post('process_year_data.php', data, function(data){
                $("#form_year").fadeOut(500).hide(function(){
                    $("#container").fadeIn(500).show(function(){
                        $("#container").html(data);
                    });
                });
            });
            posted = true;
            return false;
        }
    });
});

I have also created a JSFiddle if you use the developer tools you should see a 404 network request and then a failed redirect that hits the action. 如果您使用开发人员工具,我还创建了一个JSFiddle ,您应该看到404网络请求,然后遇到失败的重定向。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM