• Hi All,

    I’ve been trying to find a piece of AJAX code that works, or how to layout my own that successfully sends and recieves a AJAX post request from my own custom plugin.

    I started by using the code from here but I think it had something to do with the url missing. I added in ‘admin_url(‘admin-ajax.php’)’

    Sorry for the code dump, but I have no idea what an where I am going wrong.
    close_pressed and open_pressed are the php functions.
    openShop() and closeShop() are JS functions.

    add_action("wp_ajax_open_pressed","open_pressed");
    add_action("wp_ajax_close_pressed","close_pressed");
    add_action("wp_ajax_nopriv_open_pressed","open_pressed");
    add_action("wp_ajax_nopriv_close_pressed","close_pressed");
    
    function exampleMenu()
    {
    echo <<< 'EOD'
    //
    //backend settings display
    //
    <h2> Is your Shop open?</h2>
      <p id="Status">The shop is Open</p>
      <button onclick='openShop()'>Indicate Open</button>
      <button onclick="closeShop()">Indicate Closed</button>
    //
    //
    //
    //Below is the issue 
    //
    <script>
    function openShop(){
    document.getElementById("Status").innerHTML = "The shop is Open";
    jQuery(document).ready(function() {
      		var data = {
        	'action': 'open_pressed',
        	'post_type': 'POST',
        	'name': 'Close the shop'
      	};
    	
      	jQuery.post("admin_url( 'admin-ajax.php' )" , data, function(response) {
        	console.log( response );
      	}, 'json');
    	});
    }
    function closeShop(){
    document.getElementById("Status").innerHTML = "The shop is Closed";
    jQuery(document).ready(function() {
    	jQuery(document).ready(function() {
      		var data = {
        	'action': 'close_pressed',
        	'post_type': 'POST',
        	'name': 'Close the shop'
      	};
    	
      	jQuery.post("admin_url( 'admin-ajax.php' )" , data, function(response) {
        	console.log( response );
      	}, 'json');
    	});
    }
    </script>
    
    EOD;
    }
     
    function open_pressed(){
    	$isOpen = true;
    	setIsOpen($isOpen);
    	$name	= isset($_POST['name'])?trim($_POST['name']):"";
        $response	= array();
        $response['message']	= "Successfull Request";
        echo json_encode($response);
        exit;
    	
    }
    
    function close_pressed(){
    	
    	$isOpen = false;
    	setIsOpen($isOpen);
    	$name	= isset($_POST['name'])?trim($_POST['name']):"";
        $response	= array();
        $response['message']	= "Successfull Request";
        echo json_encode($response);
        exit;
    	
    }
    }

    Note: The Changing of the <p id=’status’> occurs, The javascript does run.
    When I check the chrome logs, no Successful request shows up nor does the $isOpen change, so I thus assume the problem lies within the jQuery.

    The page I need help with: [log in to see the link]

Viewing 1 replies (of 1 total)
  • Moderator bcworkz

    (@bcworkz)

    Check you browser console for errors. Any error on the page can prevent your code from working. Something on the page might be using legacy jQuery that is being deprecated. You might be able to use the Migrate Helper plugin to allow such code to work for a little longer. The plugin is a stop gap measure, the real solution is to fix the errant code.

    The plugin handbook section on Ajax might help you. The examples in the handbook, when assembled together, don’t quite work correctly, they were not intended to, they are merely illustrative. I ended up with this version of the examples that actually works (I’ve not tested lately though).

    FYI, instead of large code dumps here in the forums, use pastebin.com like I did in the last link above, or use gist.github.com

Viewing 1 replies (of 1 total)
  • The topic ‘AJAX POST request – not sending.’ is closed to new replies.