• Resolved agenciaklox

    (@agenciaklox)


    Hey guys,

    I’m developing my first wordpress plugin and i need to use Ajax, but i’ve already know how to do it.

    But i’m getting an error, i create a callback function to test with is it working, but i always get a “0” as response text.

    The code is this

    add_action( 'wp', 'wcwp_register_scripts');
    function wcwp_register_scripts( ) {
    
            wp_register_script( 'wcwp-js', plugins_url('js/wcwp_buscar_cidade.js', __FILE__), array('jquery') );
    
            wp_enqueue_script( 'jquery' );
    
    	wp_enqueue_script( 'wcwp-js' );	
    
    }
    
    function wcwp_state_proccess( ) {
    
    	echo "You selected state ". $_POST['stateId'];
    
    	exit();
    
    }

    I have a selectbox, that works well, when i change my select box, my javascript needs to get the value ‘stateId’ and returns it to me on wcwp_state_proccess(); It’s just to test if its works, but it never shows me the right value, just show me 0

    My js script is:

    jQuery(document).ready(function($) {
    
        $('#select_state_withdrawal_point').change(function(e) {
        	var state = document.getElementById("select_state_withdrawal_point");
    		var iten = state.options[state.selectedIndex].value;
        	$.post(document.location.protocol+'//'+document.location.host+'/projetos/dgiprint/wp-admin/admin-ajax.php', {action: 'wcwp_state', stateId: iten}, function(response){
    
        		alert(response);
    
        	})
    
        });
    
    });

    And to test you need to access https://marcelloruoppolo.in/projetos/dgiprint and add a product to cart, go to checkout and select “Ship to a different address?” at the end of the page will appear a ‘Withdrawal Point’ option.

    My plugin works with WooCommerce, it gives you to have a option to select a Withdrawal point, i need it for a client and don’t find anything so i decided to create it, i will share it for free on wordpress plugins when its ready ??

    If anybody can help me, i would apprecciate that ??

Viewing 1 replies (of 1 total)
  • You have to register the server-side ajax handling function (in a certain way):

    function wcwp_state_proccess( ) {
    
    	echo "You selected state ". $_POST['stateId'];
    
    	exit();
    
    }
    add_action( 'wp_ajax_wcwp_state', 'wcwp_state_proccess' );
    add_action( 'wp_ajax_nopriv_wcwp_state', 'wcwp_state_proccess' );
Viewing 1 replies (of 1 total)
  • The topic ‘Callback function doesnt works’ is closed to new replies.