duanhui5344 2019-02-01 14:37
浏览 71

在调用按钮上的函数后停止管理-ajax.php

I have a created button in my post.php screen where you can generate and download a PDF using the fpdf libary. I've got this working by using a function and then firing it if the $_POST isset. I'm then calling this on the admin_init like so:

 add_action( 'admin_init', array($this, 'my_function_with_wc_functions'));

function my_function_with_wc_functions() {
    if ( isset( $_POST['generate_posts_pdf'] ) ) {

      $post_id = $_POST['pageid'];

      if ( isset( $_POST['_close_competition_early'] ) ) {
        $complete = false;
      }

      else {
        $complete = true;
      }

      $this->output_pdf( $post_id, $complete );
    }


  <form method="post" id="as-fdpf-form">
        <input type="hidden" name="pageid" value="<?php echo $post->ID; ?>">
        <button class="button button-primary" type="submit" name="generate_posts_pdf" value="generate">Generate PDF from Competiion Entries</button>
        <button class="button button-primary" type="submit" name="generate_posts_csv" value="generate">Generate CSV from Competiion Entries</button>
      </form>


function output_pdf( $id, $complete ) {
    global $wpdb;

      if ( $complete ) {
        $table = "tickets";
      }

      else {
        $table = "tickets_regenerated";
      }

      if ( $wpdb->get_var( $wpdb->prepare( 'SELECT * FROM '.$wpdb->prefix.$table.' WHERE lottery_id= %d AND used = %d', $id, 1 ) ) ) {

      $log = $wpdb->get_results( $wpdb->prepare( 'SELECT * FROM '.$wpdb->prefix.$table.' WHERE lottery_id=%d AND used = %d', $id, 1 ) );

      global $pdf;
      $title_line_height = 10;
      $content_line_height = 8;

      $pdf->AddPage('L');
      $pdf->SetFont( 'Arial', '', 42 );

      $header = array('Order Number', 'Full name (Billing)', 'Draw #');

      $pdf->SetFont( 'Arial', 'B', 8);
      $pdf->Cell(20,5,'Order Number', 1);
      $pdf->Cell(35,5, 'Full name (billing)', 1);
      $pdf->Cell(25,5, 'Draw #' ,1);

      $pdf->Ln();

      $i = 0;
      foreach( $log as $row ) {
        if($i%2 == 0) :
            $pdf->setFillColor(255,255,255);
        else :
            $pdf->setFillColor(230,230,230);
        endif;
            $pdf->SetFont( 'Arial', '', 8 );
            $pdf->Cell(20,5,$row->order_id, 1, 0, 'C', true);
            $pdf->Cell(35,5, $row->full_name,1, 0, 'C', true);
            $pdf->Cell(25,5,$row->ticket_number,1, 0, 'C', true);
            $pdf->Ln();
       $i++;
          }
        }

    $upload = wp_upload_dir();
    $upload_dir = $upload['basedir'];
    $upload_dir = $upload_dir . '/entry-lists/'.$id;
     if (! is_dir($upload_dir)) {
       mkdir( $upload_dir, 0700 );
    }

    $pdf->Output('D', $id.'.pdf', true);
    $pdf->Output('F', $upload_dir.'/'.$id.'.pdf', true);


    exit;
}

The problem I have is that once the PDF has downloaded, the page automatically fires the admin-ajax.php and creates an autosave which causes the update button to be disabled.

I've tried adding event.preventDefault(); to the button this stops the button from working.

Is there a way I can stop the admin-ajax.php from firing after I've clicked the button?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 任务A:大数据平台搭建(容器环境)怎么做呢?
    • ¥15 r语言神经网络自变量重要性分析
    • ¥15 基于双目测规则物体尺寸
    • ¥15 wegame打不开英雄联盟
    • ¥15 公司的电脑,win10系统自带远程协助,访问家里个人电脑,提示出现内部错误,各种常规的设置都已经尝试,感觉公司对此功能进行了限制(我们是集团公司)
    • ¥15 救!ENVI5.6深度学习初始化模型报错怎么办?
    • ¥30 eclipse开启服务后,网页无法打开
    • ¥30 雷达辐射源信号参考模型
    • ¥15 html+css+js如何实现这样子的效果?
    • ¥15 STM32单片机自主设计