Reputation: 1408
I have a form containing one textfield only, and the expected behavior is that when user presses enter while in the text box the value of the text box should get submitted, but without re-page (refresh) load. I have tried various ways of setting AHAH but still unable to achieve the expected behavior, the page reloads. I have searched for any solution to this problem without luck.
Please advice how it has to be done or direct me somewhere useful Here is code:
function user_porfile_message_form(){
$form = array();
$form['element1'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'no-js'),
'#ahah' => array(
'path' => 'module/file/callback',
'wrapper' => 'some-wrapper',
'event' => 'click',
),
);
return $form;
}
Upvotes: 0
Views: 540
Reputation: 1408
Got the solution, here it is:
function some_form(&$form_state){
$form = array();
$form['text'] = array(
'#value' => "Text",
'#prefix' => '<div id="some-wrapper">',
'#suffix' => '</div>',
);
$form['text_input'] = array(
'#type' => 'textfield',
'#title' => '',
'#default_value' => '',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#attributes' => array('class' => 'no-js'),
'#ahah' => array(
'path' => 'module/file/callback',
'wrapper' => 'some-wrapper',
'event' => 'click',
),
);
return $form;
}
function some_form_submit(){
//Do whatever has to be done for submission
}
function some_form_callback(){
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
$form = form_get_cache($form_build_id, $form_state);
$args = $form['#parameters'];
$form_id = array_shift($args);
$form_state['post'] = $form['#post'] = $_POST;
$form['#programmed'] = $form['#redirect'] = FALSE;
drupal_process_form($form_id, $form, $form_state);
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);
$changed_elements = $form['text'];
unset($changed_elements['#prefix'], $changed_elements['#suffix']);
drupal_json(array(
'status' => TRUE,
'data' => drupal_render($changed_elements),));
}
/**
* Implement hook_menu().
*/
function module_menu(){
$items['module/file/callback'] = array(
'page callback' => 'some_form_callback',
'access callback' => TRUE,
'file' => 'module.module',
'type' => MENU_CALLBACK,
);
return $items;
}
Upvotes: 0