Обновление AJAX Wordpress ACF с идентификатором нажатия кнопки - PullRequest
0 голосов
/ 09 сентября 2018

Я использую ACF WordPress для обновления поля ACF, чтобы пользователь мог заполнить его нажатием кнопки id. Я получаю сообщение об ошибке как admin-ajax.php с 400 Ошибка POST https://domainname/wp-admin/admin-ajax.php 400 ()

WordPress код на шаблоне

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

**

Код JJery AJAX

**

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

});

**

Код Function.php

**

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 = ''); 

} 

Обновлен jquery с действием

jQuery(function($){

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

});
...