1

I am inserting a record in a temp table using ajax callback. but I want to clear the whole form when the data is submitted using ajax. In this code I am returning only the grid return $form['traveler_itinerary']['temp_report'];. It only reload the grid but when i try to reset the whole fieldset $form['traveler_itinerary'], the full form regenerate again while the previous one is already there. Please guide me how to reset the form when the data is submitted using ajax.

    <?php

    function emp_travel_request_menu()
    {
$items  = array();
// Generate a changing number of checkboxes.
$items['emp_travel_request'] = array(
    'title' => 'Employee Travel Request',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 1,

);

$items['emp_travel_request/hotel'] = array(
    'title' => 'Hotel Reservation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 2,
    'type' => MENU_DEFAULT_LOCAL_TASK,
);

$items['emp_travel_request/logistics'] = array(
    'title' => 'Logistics',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
);

$items['emp_travel_request/airticket'] = array(
    'title' => 'Hotel Reservation',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 4,
    'type' => MENU_LOCAL_TASK,
);

$items['emp_travel_request/security'] = array(
    'title' => 'Security',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 5,
    'type' => MENU_LOCAL_TASK,
);

$items['emp_travel_request/finance'] = array(
    'title' => 'Finance',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('emp_travel_request_form'),
    'access callback' => true,
    'weight' => 6,
    'type' => MENU_LOCAL_TASK,
);



return $items;
}

 /**
 * Form manipulation through AJAX.
 *
  * AJAX-enabled select element causes replacement of a set of checkboxes
 * based on the selection.
  */
function emp_travel_request_form($form, &$form_state) {

 //Attach the CSS for the form
$form['#attached']['css'] = array(
    drupal_get_path('module', 'emp_travel_request') . '/travel_style.css',
);

//Get the list of all the employees
$user_query = db_select('users');
$user_query->fields('users', array('uid', 'name',));
$user_query->condition('name','','<>');
$user_query->condition('name','hr.admin','<>');
$user_query->condition('name','admin','<>');
$user_query->orderBy('name', 'ASC');
$user_results = $user_query->execute();

//define rows
$uid_options = array();
foreach ($user_results as $user_result) {
    $uid_options[$user_result->uid] = $user_result->name;
}

$form['travelers_info_tabs'] = array(
   '#type' => 'vertical_tabs',
);

$form['travelers_info'] = array(
    '#type' => 'fieldset',
    '#title' => t('Travel Request Form'),
    '#weight' => 1,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,

);

$form['travelers_info']['all_travelers'] = array(
    '#title' => t('Traveler / Traveler\'s'),
    '#type' => 'checkboxes',
    '#options' => $uid_options,
    //'#default_value' => $default,
    '#prefix' => '<div class="all_travelers">',
    '#suffix' => '</div>',
    '#required' => TRUE,
    '#ajax' => array(
        'callback' => 'all_travelers1_callback',
        'wrapper' => 'all-travelers-div',
        'method' => 'replace',
        'effect' => 'fade',
    ),

);

$form['travelers_info']['reason_for_travel'] = array(
    '#title' => t('Reason for Travel'),
    '#type' => 'textfield',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 2,
    '#required' => FALSE,

);

$form['traveler_itinerary'] = array(
    '#type' => 'fieldset',
    '#title' => t('Traveler Itinerary'),
    '#weight' => 2,
    '#collapsible' => TRUE,
    '#collapsed' => FALSE,

);


$form['traveler_itinerary']['who_travelers'] = array(
    '#title' => t('Who'),
    '#type' => 'fieldset',
    '#prefix' => '<div id="all-travelers-div">',
    '#suffix' => '</div>',
    '#weight' => 2,
    '#required' => FALSE,
    '#tree' => TRUE,
);

$format = 'dd/mm/YYYY';

$form['traveler_itinerary']['travel_date'] = array(
    '#type' => 'date_select', // types 'date_text' and 'date_timezone' are also supported. See .inc file.
    '#title' => t('Travel Date'),
    '#default_value' => date_now(),
    '#date_format' => $format,
    '#date_label_position' => 'within', // See other available attributes and what they do in date_api_elements.inc
    '#date_increment' => 15, // Optional, used by the date_select and date_popup elements to increment minutes and seconds.
    '#date_year_range' => '-3:+3', // Optional, used to set the year range (back 3 years and forward 3 years is the default).
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 1,
    '#required' => FALSE,

);


$form['traveler_itinerary']['travel_from'] = array(
    '#type' => 'select',
    '#options' =>array(
        'Islamabad' => t('Islamabad'),
        'Peshawar' => t('Peshawar'),
        'Lahore' => t('Lahore'),
        'Karachi' => t('Karachi'),
        'Quetta' => t('Quetta'),
    ),
    '#title' => t('Travel From'),
    '#empty_option' => '--Select Location--',
    '#prefix' => '<div id="travel-from-div">',
    '#suffix' => '</div>',
    '#weight' => 3,
    '#required' => FALSE,

);

$form['traveler_itinerary']['travel_to'] = array(
    '#type' => 'select',
    '#options' =>array(
        'Islamabad' => t('Islamabad'),
        'Peshawar' => t('Peshawar'),
        'Lahore' => t('Lahore'),
        'Karachi' => t('Karachi'),
        'Quetta' => t('Quetta'),
    ),
    '#title' => t('Travel To'),
    '#empty_option' => '--Select Location--',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 4,
    '#required' => FALSE,

);

$form['traveler_itinerary']['mode'] = array(
    '#title' => t('Mode'),
    '#type' => 'radios',
    '#options' =>array(
        'By Road' => t('By Road'),
        'By Train' => t('By Train'),
        'By Plane' => t('By Plane'),

    ),
    '#default_value' => 'By Road',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 5,
    '#required' => FALSE,

);

$form['traveler_itinerary']['airport'] = array(
    '#title' => t('Airport Pick & Drop Req.'),
    '#type' => 'checkbox',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 6,
    '#required' => FALSE,

);

$form['traveler_itinerary']['acc'] = array(
    '#title' => t('Accomodation req.'),
    '#type' => 'checkbox',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#weight' => 7,
    '#required' => FALSE,

);

$form['traveler_itinerary']['pickup_point'] = array(
    '#title' => t('Pickup Point'),
    '#type' => 'textfield',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#size' => 60,
    '#maxlength' => 128,
    '#weight' => 8,
    '#required' => FALSE,

);


$form['traveler_itinerary']['pickup_time'] = array(
    '#title' => t('Pickup Time'),
    '#type' => 'textfield',
    '#prefix' => '<div>',
    '#suffix' => '</div>',
    '#size' => 60,
    '#maxlength' => 128,
    '#weight' => 9,
    '#required' => FALSE,

);

//session_start();    

$form['traveler_itinerary']['travel_session_id'] = array(
    '#type' => 'hidden',
    '#value' => session_id(),

);



//add the users to the who travelers list box
if(!empty($form_state['values']['all_travelers'])) {

    $travelers_ID = array_filter($form_state['values']['all_travelers']);

    //Get the list of selected the employees
    $user_query = db_select('users');
    $user_query->fields('users', array('uid', 'name',));
    $user_query->condition('uid', $travelers_ID, 'IN');
    $user_results = $user_query->execute();

    //add checkboxes to the who_travel fieldset
    foreach ($user_results as $user_result) {
        $form['traveler_itinerary']['who_travelers']["$user_result->uid"] = array(
            '#type' => 'checkbox',
            '#title' => $user_result->name,
            '#return_value' => $user_result->uid,
        );

    }

 }




$markup = '';



$markup = '<thead>
    <tr>
        <th>Date</th>
        <th>Who</th>
        <th>From</th>
        <th>To</th>
        <th>Mode</th>
        <th>Airport pick & drop req.</th>
        <th>Acc.</th>
        <th>Pickup Point</th>
        <th>Pickup Time</th>
      </tr>
    </thead><tbody>';



//Adding the record to the temprery table
if(!empty($form_state['values']['travel_from']))
{
    $travel_date = !empty($form_state['values']['travel_date']) ? $form_state['values']['travel_date'] : '';
    $location_from = !empty($form_state['values']['travel_from']) ? $form_state['values']['travel_from'] : '';
    $location_to = !empty($form_state['values']['travel_to']) ? $form_state['values']['travel_to'] : '';   
    $mode_of_travel = $form_state['values']['mode'];     
    $airport_pick_drop_req = $form_state['values']['airport'];
    $accomodation_req = $form_state['values']['acc'];
    $pickup_point = !empty($form_state['values']['pickup_point']) ? $form_state['values']['pickup_point'] : '';                 
    $pickup_time = !empty($form_state['values']['pickup_time']) ? $form_state['values']['pickup_time'] : '';                    
    $travel_session_id = !empty($form_state['values']['travel_session_id']) ? $form_state['values']['travel_session_id'] : '';

    $travels_who = array_filter($form_state['values']['who_travelers']);
    foreach ($travels_who as $key => $value) {
      $travel_who_selected .=  $value . "," ;         
    }


    $updated = db_insert('travel_request_itinerary_tmp')
                ->fields(array(
                'travel_date' => $travel_date,
                'travels_who' => $travel_who_selected,
                'location_from' => $location_from,
                'location_to' => $location_to,
                'mode_of_travel' => $mode_of_travel,
                'airport_pick_drop_req' => $airport_pick_drop_req,
                'accomodation_req' => $accomodation_req,
                'pickup_point' => $pickup_point,
                'pickup_time' => $pickup_time,
                'travel_session_id' => $travel_session_id,
                ))
                ->execute();




}

//add the rows of the travel_request_itinerary_tmp based seession_id to the table
$travel_temp_query = db_select('travel_request_itinerary_tmp');
$travel_temp_query->fields('travel_request_itinerary_tmp', array(
    'travel_session_id', 
    'travel_date',
    'travels_who',
    'location_from',
    'location_to',
    'mode_of_travel',
    'airport_pick_drop_req',
    'accomodation_req',
    'pickup_point',
    'pickup_time',
    'tmp_id',
    ));

$travel_temp_query->condition('travel_session_id', session_id(), '=');
$travel_temp_results = $travel_temp_query->execute();

  //add checkboxes to the who_travel fieldset
foreach ($travel_temp_results as $travel_temp_result) {
    $markup .= '<tr>
                <td>' . date('d-m-Y', strtotime($travel_temp_result->travel_date)) . '</td>
                <td>' . get_travelers_name($travel_temp_result->travels_who) . '</td>
                <td>' . $travel_temp_result->location_from . '</td>
                <td>' . $travel_temp_result->location_to . '</td>
                <td>' . $travel_temp_result->mode_of_travel . '</td>
                <td>' . $travel_temp_result->airport_pick_drop_req = 0 || is_null($travel_temp_result->airport_pick_drop_req) ? "No" : "Yes" . '</td>
                <td>' . $travel_temp_result->accomodation_req = 0 || is_null($travel_temp_result->accomodation_req) ? "No" : "Yes" . '</td>
                <td>' . $travel_temp_result->pickup_point . '</td>
                <td>' . date ('H:i',strtotime($travel_temp_result->pickup_time)) . '</td> 
                </tr>';
}


   $markup .= '</tbody>'; 

$form['traveler_itinerary']['temp_report'] = array(
               '#markup' => '<table width="100%" border="1">'. $markup .'</table><br/><br/>',
               '#prefix' => '<div id="temp-wrapper">',
               '#suffix' => '</div>', 
               '#weight' => 10,
                 );


 $form ['traveler_itinerary']['btn_travelers']    = array(
  '#type' => 'button',
  '#value' => t('Add Travelers'),
  '#weight' => 11,
  '#ajax' => array(
    'event' => 'click',
    'callback' => 'add_travelers_callback',
    'wrapper' => 'temp-wrapper',
    'method' => 'replace',
   ),
 );

$form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Submit'),
    '#weight' => 3,
);


return $form;
}

function get_travelers_name($travelers_ids){
$travelers_ids = rtrim($travelers_ids,','); 
$query = db_select('users', '');
$query->fields('users', array('name'));
$query->condition('uid', array(explode(',',$travelers_ids)), 'IN');
$results = $query->execute();

$name_array = "<ul>";    
foreach ($results as $result) {
    $name_array .= "<li>" . str_replace(".", " ", $result->name) . "</li>" ;
}
$name_array .= "</ul>";


return rtrim($name_array,', ');

}


function add_travelers_callback($form, &$form_state) {
if (!empty($form_state['executed'])) {
    $form['result'] = array(
        '#markup' => 'Done!</b>',
    );
    $form['pickup_point']['#value'] = '';
    return $form['traveler_itinerary'];
} else {
    $form['result'] = array(
        '#markup' => 'Error!</b>',
    );
}
return $form['traveler_itinerary']['temp_report'];
//$form_state['rebuild'] = TRUE;
 //return $form['traveler_itinerary']; 
}



function all_travelers1_callback($form, $form_state) {
return $form['traveler_itinerary']['who_travelers'];
  // return $form;
}




function emp_travel_request_form_submit($form, &$form_state){
$form_state['rebuild'] = TRUE;
}

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.