Support

Account

Home Forums Backend Issues (wp-admin) Use ACF field selection to update another ACF field Reply To: Use ACF field selection to update another ACF field

  • Hi thanks again for this. I just wrote a really long reply but it disappeared. I didn’t have any luck even with the basic example in ‘AJAX in plugins’

    Just to check theres not anything glaringly obviously wrong heres my code…

    In functions.php

    add_action( 'admin_enqueue_scripts', 'my_enqueue' );
    function my_enqueue() {
            
    	wp_enqueue_script( 'ajax-script', get_template_directory_uri() . '/js/my_query.js', array('jquery') );
    
    	// in JavaScript, object properties are accessed as ajax_object.ajax_url, ajax_object.we_value
    	wp_localize_script( 'ajax-script', 'ajax_object',
                array( 'ajax_url' => admin_url( 'admin-ajax.php' ), 'we_value' => 1234 ) );
    }

    In my_query.js

    jQuery(document).ready(function($) {
    	
    	console.log('myquery');
    	
    	var data = {
    		'action': 'my_action',
    		'whatever': ajax_object.we_value      // We pass php values differently!
    	};
    	// We can also pass the url value separately from ajaxurl for front end AJAX implementations
    	jQuery.post(ajax_object.ajax_url, data, function(response) {
    		alert('Got this from the server: ' + response);
    	});
    });

    and at the bottom of admin-ajax.php

    // Same handler function...
    add_action( 'wp_ajax_my_action', 'my_action' );
    function my_action() {
    	global $wpdb;
    	$whatever = intval( $_POST['whatever'] );
    	$whatever += 10;
            echo $whatever;
    	wp_die();
    }	

    I just get a 400 error

    But I think I’m going to give up and pay someone to help me. I guess you probably have better things to do but let me know if not. Its really not working for me