Friday, 30 October 2015

"Error: Invalid form POST data" in ajax login form

 When using ajax login form we are getting the warning "Error: Invalid form POST data" to resolve that we need to implement hook_exit()

 * Implements hook_exit().
function module_exit($destination = NULL) {
  if (arg(0) == 'system' && arg(1) == 'ajax') {
    $is_user_login_form_submission = isset($_POST) && isset($_POST['name']) && isset($_POST['pass']) && isset($_POST['form_build_id']);
    if ($is_user_login_form_submission && user_is_anonymous()) {
      $form_build_id = $_POST['form_build_id'];
      $form_state = form_state_defaults();
      $form_state['values'] = $_POST; // Important!
      $form = form_get_cache($form_build_id, $form_state);

      if (!$form) {
        watchdog(__FUNCTION__, 'User login AJAX form submission failed. Trying again...', array(), WATCHDOG_WARNING);

        $form = drupal_rebuild_form('user_login_form', $form_state);
        $form['#build_id_old'] = $form['#build_id']; // Important!

        // Try form submission again after it is rebuilt above
        $commands[] = ajax_command_update_build_id($form);
        $commands[] = ajax_command_invoke('form#user-login-form', 'trigger', array('submit'));

        print ajax_render($commands);

Refference Link

Tuesday, 20 October 2015

Drupal update the user email and name using user id UID

This is a small function used to update the user email
@param $uid Is the user id of the particular user
@param $email New email address to change to the particular user UID
function UpdateUserEmail($uid, $email){
  // load user object
$existingUser = user_load($uid);

// create an array of properties to update you can add what ever user property you want to edit in this array.
$edit = array(
  'name' => $email,
  'mail' => $email,
  'init' => $email,

// save existing user
    (object) array('uid' => $existingUser->uid),
// load user object

Refference links

Friday, 2 October 2015

How to validate and submit a form using AJAX in Drupal ?

Drupal convert your form in to ajax 
Below is the example of ajaxifying your drupal form. all you have to do is write the call back
wrapper Form api will ajaxify your form.  
function test_form($form, &$fstate) {
  $form["wrapper"] = array("#markup" => "<div id='test-ajax'></div>");
  $form["name"] = array("#type" => "textfield", "#required" => true, "#title" => "Name");
  $form["submit"] = array(
    "#type" => "submit", 
    "#value" => "Send", 
    "#ajax" => array(
    "callback" => "test_form_callback", 
    "wrapper" => "test-ajax",
    "effect" => "fade"));
  return $form;

function test_form_callback($form, &$fstate) {
  return "<div id='test-ajax'>Wrapper Div</div>";

function form_validate($form, &$fstate) {
  form_set_error("name", "your  error to display.");
Reference Links: