AJAX Wordpress ACF update with button click id

I using ACF wordpress to update acf field for user to populate with button id click.
I am getting Error as admin-ajax.php with 400
Error POST https://domainname/wp-admin/admin-ajax.php 400 ()

WordPress code on template

<button class="get_project" id="1479" name="urbtnid" data-postid="147">Get project</button>
<button class="get_userbtn" value="<?php echo get_current_user_id() ?>" id="get_userbtn" ><?php echo get_current_user_id() ?></button>

**

JQUery AJAX Code

**

  jQuery(function($){
    $('.get_project').click(function() {
      var stdid = jQuery('#get_userbtn').attr('value');
      var stdbtnid = this.id;;
      var dataString = 'stdid1=' + stdid + '&stdbtnid1=' + stdbtnid; 
       jQuery.ajax({
             url: my_ajax_object.ajax_url,
            type : 'post',
             data: dataString,
            success : function( response ) {
                 console.log(response);
            }
        });
    })

});

**

Function.php code

**

function my_enqueue_ajax_save() {

    wp_register_script( 'ajax-script', get_template_directory_uri() . '/pradeep-js-css/my-ajax-id-save-script.js', array('jquery') , false, true );
     wp_enqueue_script( 'ajax-script' );

    wp_localize_script( 'ajax-script', 'my_ajax_object',
            array( 'ajax_url' => admin_url( 'admin-ajax.php' ) ) );

}
add_action( 'wp_enqueue_scripts', 'my_enqueue_ajax_save' );
add_action( 'wp_ajax_my_action', 'my_action' );
add_action( 'wp_ajax_nopriv_my_action', 'my_action' ); 

function my_action(){
    $postid = $_POST['stdid1'];;
    $fieldname = 'last_question_viewed'; 
    $fieldvalue = $_POST['stdbtnid1'];          
    update_post_meta($postid, $fieldname, $fieldvalue);
    echo $postid;
    wp_die($fieldname = '', $fieldvalue = '', $postid = ''); 

} 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s