0

My problem is the same as in other cases, but my code is fine, I compared with another and followed all the instructions and good practices, but it does not work. Here my code:

jQuery("#genera-pdf").click(function(e){
  e.preventDefault();
  var price1 = jQuery('input[type=text][id=price1]').val();
  var price2 = jQuery('input[type=text][id=price2]').val();

   jQuery.ajax({
               url : '/wp-admin/admin-ajax.php',
               type: 'POST',
               data: {action: 'pdfAction', a: price1, b: price2}),
               success: function(data){
                   alert(data);     
               },
               error: function(errorThrown){
                   alert(errorThrown);
               }
             });
          });

And my function is:

function pdfAction(){
$post = $_POST['pdfAction'];
echo "something";

wp_die();

}
add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

Any idea that is happening?

  • Have you tried using the REST API for ajax instead? It's a much better API and actually tells you what the problem is in plain english, rather than returning 0 with no explanation – Tom J Nowell Jun 11 at 15:06
  • Please, how I do this? – Mirquez Rodriguez Jun 19 at 17:03
2

You function name and your ajax action callback name should be same.

add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

function pdfAction_callback(){
    $post = $_POST['pdfAction'];
    echo "something";

    wp_die();

}

Please update your php code with the above.

Currently, your php function is not getting executed.

Update:

The Ajax Script,

$(document).on('click', "#generate-pdf", function(){

    var price1 = $("#price1").val();
    var price2 = $("#price2").val();

    $.ajax({
        url: ajax_object.ajax_url, // AJAX handler
        data: {
            'action': 'pdfAction',
            'price_1': price1,
            'price_2': price2
        },
        type: 'POST',
        success: function(data) {

            console.log(data);
            console.log(data.price_1) // to access individual values

        },
        error: function(errorThrown){
            console.log(errorThrown);
        }
    });
    return false;

});

Register Ajax Action Url and Script,

function ajax_enqueue_scripts() {

    // Enqueue your javascript file 
    wp_register_script( 'ajax-scripts', get_template_directory_uri() . '/js/ajax-scripts.js', array('jquery'), '1.0.0', true );

    // in JavaScript, object properties are accessed as ajax_object.ajax_url
    wp_localize_script( 'ajax-scripts', 'ajax_object', array( 
        'ajax_url' => admin_url( 'admin-ajax.php' ), // admin ajax url 
    ));

    wp_enqueue_script('ajax-scripts');

}

add_action( 'wp_enqueue_scripts', 'ajax_enqueue_scripts' );

Ajax Request Callback function,

add_action('wp_ajax_pdfAction','pdfAction_callback');
add_action('wp_ajax_nopriv_pdfAction','pdfAction_callback');

function pdfAction_callback(){

    // Getting data sent from Ajax request

    $price_1 = $_POST['price_1'];
    $price_2 = $_POST['price_2'];


    // Prepare Data to be send back to AJAX request
    $return = array(
        'price_1'  => $price_1,
        'price_2'  => $price_2
    );

    wp_send_json($return); // Send JSON response back to our AJAX Request and die().

    // we don't require wp_die(); here wp_send_json() will take care of it.

}

You also should consider using nonce here for security purpose.

  • I did but does not work. – Mirquez Rodriguez Jun 11 at 15:24
  • As @MikeNGarrett stated your code has lots of errors, check my update in answer to learn how to use ajax properly in WordPress. – Deepak Singh Jun 11 at 15:58
  • Thanks for improving upon my answer. – MikeNGarrett Jun 11 at 16:34
  • @DeepakSingh I applied the change in the code but it does not work, and an error is shown in the console ajax_object is not defined – Mirquez Rodriguez Jun 11 at 18:21
  • @MirquezRodriguez please check my answer I have updated ajax_enqueue_scripts() function. – Deepak Singh Jun 11 at 18:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.