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));
}
?>

Comments

sam.clayton@gmail.com’s picture

I'd like to know the same thing. I'll be working on something related today.

maozet’s picture

you should try $_POST

Jaypan’s picture

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
}

I will soon be leaving the Drupal forums permanently. To understand why, please see this thread.

asmdec’s picture

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.

KeyboardCowboy’s picture

I'm not sure if this is any more secure than calling the $_POST variable directly, but it follows the tutorial in [#331941]. You could call this subroutine from within your AHAH callback function to get an array of the data entered into your form without submitting the form.

<?php
function MODULE_get_ahah_form_data() {
 
$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);

 
// Preparing for #5.
 
$args = $form['#parameters'];
 
$form_id = array_shift($args);
 
$form_state['post'] = $form['#post'] = $_POST;
 
$form['#programmed'] = $form['#redirect'] = FALSE;

 
// Step #5.
 
$form = drupal_rebuild_form($form_id, $form_state, $args, $form_build_id);

  return
$form['#parameters'][1]['post'];
}
?>

--------
Chris (KeyboardCowboy) - @ChrisAlbrecht