调用按钮上的函数后,停止触发admin-ajax.php

时间:2019-02-01 14:37:32

标签: javascript php jquery ajax wordpress

我在post.php屏幕上创建了一个按钮,您可以在其中使用fpdf库生成和下载PDF。我已经通过使用一个函数来工作,如果设置了$ _POST,则将其触发。然后,我在admin_init上这样称呼:

 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;
}

我遇到的问题是,一旦PDF下载完毕,页面就会自动触发admin-ajax.php并创建自动保存功能,从而导致更新按钮被禁用。

我尝试将event.preventDefault();添加到按钮上,这使按钮无法正常工作。

有没有办法可以在单击按钮后阻止admin-ajax.php触发?

0 个答案:

没有答案