Search code examples
phpajaxjqueryjconsole

Ajax Doesn't give me anything in the console?


Updated Question to better reflect the communities support

Based on community support I have changed the Ajax function to be as such:

(function($){   
    $(document).ready(function(){
        $('a').click(function(e){
          var el = $(this).prev('input[type="checkbox"]');
          if(el.is(':checked')){
               el.prop('checked',false);   
          }
          $.ajax({
              url  : "http://localhost/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/AdminPanel/Template/Helper/UncheckPackageThemeHelper.php",
              type : 'GET',
              data : { 'element_name' : el.prop('name') },      
              success: function(data, textStatus, jqXHR){
                console.log(data);
              },
              error: function(jqXHR, textStatus, errorThrown ){
                console.log(jqXHR, textStatus, errorThrown);
              } 
          });
          e.preventDefault();
        });
    }); 
 })(jQuery);

The resulting PHP Class is as such:

class CoreTheme_AdminPanel_Template_Helper_UncheckPackageThemeHelper{

    private $_element_name = null;

    public function __construct(){
        if(isset($_GET['element_name'])){
            $this->_element_name = $_GET['element_name'];
            echo $this->_element_name;
        }
    }
}

The network tab shows that I have some out put from activating the Jquery which I have shown below:

enter image description here

The Console is spitting out no errors, yet not echoing the element name. I have read up on the Jquery Ajax API and everything I have been doing, thus far, seems to be correct. Yet I am not getting the desired out put.

Note: The response tab is empty.... In other words I am not getting a response back.


Solution

  • So I was doing a lot of things wrong. But this is the only way it works, for me - please post your comments if you have better solution.

    In the class, I have to do this:

    class CoreTheme_AdminPanel_Template_Helper_UncheckPackageThemeHelper{
    
        private $_element_name = null;
    
        public function __construct(){
            if(isset($_GET["element_name"])){
                $this->_element_name = $_GET["element_name"];
                echo json_encode($this->_element_name);
            }
        }
    }
    
    new CoreTheme_AdminPanel_Template_Helper_UncheckPackageThemeHelper();
    

    The class cannot be instantiated in the .phtml file with the resulting Ajax or the request is never sent back. Also notice the json_encode You cannot pass regular variables back to Ajax - when in a class (I think) - How ever when not in a class it seems to work - clarification is needed.

    The Ajax then looks like this:

    (function($){   
        $(document).ready(function(){
            $('a').click(function(e){
              var el = $(this).prev('input[type="checkbox"]');
              if(el.is(':checked')){
                   el.prop('checked',false);   
              }
              $.ajax({
                  url  : "http://localhost/wordpress/wp-content/themes/Aisis-Framework/CoreTheme/AdminPanel/Template/Helper/UncheckPackageThemeHelper.php",
                  type : 'GET',
                  data : { 'element_name' : el.prop('name') },      
                  success: function(data, textStatus, jqXHR){
                    console.log(data);
                  },
                  error: function(jqXHR, textStatus, errorThrown ){
                    console.log(jqXHR, textStatus, errorThrown);
                  } 
              });
              e.preventDefault();
            });
        }); 
     })(jQuery);
    

    The Data that comes back is what I expect: "aisis_options[package_RelatedPosts]"

    There ar a couple of issues with this answer - One I dont understand why I have to instantiate the class inside the file its written in, and two not sure why I have to encode the response in a class, but not when its just a "script kiddy" file.