How to log in a user with jQuery Ajax in Joomla
If your are a Joomla developer sometimes you encounter a task which needs to produce jQuery Ajax call in order to log in a user to a Joomla CMS based site.
In this case this article will help you to implement this feature on your site.
Then add HTML form to your page.
Log in User form sample HTML code:
Then create an empty file called: view.ajax.php and upload it to YOUR_SITE_HOME/components/com_content/views/article folder
Add the php code below into this file:
_registerNewUser( JFactory::getApplication()->input->post->get('form', NULL, 'ARRAY') );
break;
}
jexit();
}
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();
}
}
?>
Then create a java script file with this JS script and add it to your page HEADER or just add it to your article php source:
Sample Log In User form:
Click the button above to see how it makes a jQuery Ajax call and logs in a Joomla user.
Joomla Articles