How to get form values from AHAH callback without submit?
BrianH - June 22, 2009 - 07:57
In the example below, how would I get the value entered into $form['element'] that triggers the AHAH callback, without actually submitting the form?
<?php
function test_menu() {
$items['test'] = array(
'title' => 'Test',
'page callback' => 'drupal_get_form',
'page arguments' => array('test_form'),
'access arguments' => array('access content')
);
$items['test/message_js'] = array(
'page callback' => 'test_message_js',
'type' => MENU_CALLBACK,
'access arguments' => array('access content')
);
return $items;
}
function test_form() {
$form['element'] = array(
'#title' => t('Text Entry'),
'#type' => 'textfield',
'#maxlength' => 4,
'#required' => TRUE,
'#ahah' => array(
'event' => 'blur',
'path' => 'test/message_js',
'wrapper' => 'target',
'effect' => 'fade',
),
);
$form['target'] = array(
'#type' => 'markup',
'#prefix' => '<div id="target">',
'#value' => t('Enter something above to see a dropdown list here.'),
'#suffix' => '</div>',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Submit'),
);
return $form;
}
function test_message_js() {
// How do I get access to $form['element'] value from here, without requiring a form submit?!
// do something with value of $form['element']
$form_portion['dropdown'] = array(
'#title' => t('Drop-down list'),
'#type' => 'select',
'#maxlength' => 50,
'#options' => array(
'One',
'Two',
'Three',
'Four',
),
'#required' => TRUE,
);
$output = drupal_render($form_portion);
drupal_json(array('status' => TRUE, 'data' => $output));
}
?>
Ditto
I'd like to know the same thing. I'll be working on something related today.
try $_POST[''element']
you should try $_POST
Maozet
www.drupaldir.com
I'm still a fair newb at
I'm still a fair newb at ahah, but I believe that you aren't supposed to use $_POST values as it is insecure (drupal 6).
I think that what you want to do is change this:
function test_form() {to this
function test_form(&$form_state) {Which gives you access to the values that were entered before the ahah handler was called in $form_state. You can then use conditionals based on $form_state to build your form. For example:
if(!isset($form_state['something'])){
// output original form element
}
else
{
// output new form element
}
You could get the form from
You could get the form from the cache:
function test_form() {
$form = array(
'#cache' => TRUE,
);
// ... the rest of original code
}
function test_message_js() {
// How do I get access to $form['element'] value from here, without requiring a form submit?!
$form_state = array('storage' => NULL, 'submitted' => FALSE);
$form_build_id = $_POST['form_build_id'];
// Get the form from the cache.
$form = form_get_cache($form_build_id, $form_state);
// do something with value of $form['element']
// ... the rest of original code
}
For more information see Doing AHAH Correctly in Drupal 6 here http://drupal.org/node/331941.