Support

Account

Home Forums Backend Issues (wp-admin) I using ACF wordpress to update acf field for user to populate with button id cl

Unread

I using ACF wordpress to update acf field for user to populate with button id cl

  • I using ACF wordpress to update acf field for user to populate with button id click.

    ************** 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($){

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

    });
    ********************** 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 = ”);

    }

Viewing 1 post (of 1 total)

The topic ‘I using ACF wordpress to update acf field for user to populate with button id cl’ is closed to new replies.