I've been following this tutorial to make an AJAX login form. http://www.mysite4u.net/joomla-articles/jquery-ajax-joomla-login.html

But I can't get it to work. The ajax call dosen't work. It gives me this error Uncaught TypeError: Cannot read property 'loggedIn' of null

I tried reading this documentation, but I guess that it is not the AJAX component that is used in this, and it dosen't mention anything about calling components: https://docs.joomla.org/Using_Joomla_Ajax_Interface

This is my AJAX call:

form = jQuery('#userForm'); 

jQuery.ajax({
    url: 'index.php?option=com_content&format=ajax&view=article&task=loginUser&tmpl=component',
    type: 'post',
    dataType: 'html',
    data: {
        form: jQuery(form).serializeArray()
    },
    async: true,
    success: function(response){
        console.log(response);
        var result = jQuery.parseJSON(response);

        if ( result.loggedIn == 1 ) {                   
            //User logged in                    
        }
        else {                  
            //User not logged in                    
        }               
    }
});

And the function inside components/com_content/views/article/view.ajax.php The "view.ajax.php" is a file I've created:

public function _logInUser( $form ){
    $options = array();
    $credentials = array();

    $credentials['username'] = $form[0]['value'];
    $credentials['password'] = $form[1]['value'];

    $result = JFactory::getApplication()->login($credentials, $options);        
    $result = ($result) ? 1 : 0;

    //1     - logged in
    //0     - not logged in     
    echo json_encode( array('loggedIn' => $result) );                   
    jexit();

}

Apperently the tutorial was missing an important part. It needed to have the "loginUser" task addded to the switch in view.ajax.php like:

function display($tpl = null)
{       
    $task = JRequest::getString('task');        
    switch( $task ){                        
        case "registerNewUser":         
            $this->_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );             
            break;
        case "loginUser":           
            $this->_logInUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );               
            break;
    }       
    jexit();                
}

Maybe I misunderstand you in your question but in the directory of com_content/views/article you cannot/should not create a view.html.php file since it originally has a good view.html.php file.

In the tutorial which you follow the author says that you should create an 'view.ajax.php' file in com_content/views/article folder (not a view.html.php file). Maybe that is causing your problem since you created a wrong file for this ajax call.

And if that is just a mistake in your question, then you also might want to study this earlier answer about ajax calls within Joomla here (it is better not to repeat that here): What is the proper way to make an AJAX call in component?

  • Woops, my fault... It is a view.ajax.php I've created. Thanks for your suggestion! – JonasB Oct 12 at 10:12
  • I added a link in my answer about the proper ajax calls in Joomla, if you did not study that before. – Zollie Oct 12 at 10:24

Your Answer

 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.