drupal 7 ajax framework example does not work

72 views Asked by At

I want to add one form in user profile page. So i created user-profiled.tpl.php. In that template,i just copied the example from ajax framework for testing. Here is the whole codes:

function ajax_example_simplest($form, &$form_state) {
 $form = array();
 $form['changethis'] = array(
'#type' => 'select',
'#options' => array(
  'one' => 'one',
  'two' => 'two',
  'three' => 'three',
),
'#ajax' => array(
  'callback' => 'ajax_example_simplest_callback',
  'wrapper' => 'replace_textfield_div',
 ),
);


$form['replace_textfield'] = array(
'#type' => 'textfield',
'#title' => t("The default value will be changed"),
'#description' => t("Say something about why you chose") . "'" .
  (!empty($form_state['values']['changethis'])
  ? $form_state['values']['changethis'] : t("Not changed yet")) . "'",
'#prefix' => '<div id="replace_textfield_div">',
'#suffix' => '</div>',
);
  return $form;
 }

function ajax_example_simplest_callback($form, $form_state) {
 return $form['replace_textfield'];
}

I am confusing now. After i clicked the checkbox, nothing change in textfield. My jquery version is 1.9. Is there any other constraints?

1

There are 1 answers

0
Triss On

The change is in description and not in textfield(I did the same error first as you did). However I have updated the code so it also shows the value in 'textfield'.

function ajax_example_simplest($form, &$form_state)
{
    $form = array();

    $form['changethis'] = array(
        '#type' => 'select',
        '#options' => array(
            'one'   => 'one',
            'two'   => 'two',
            'three' => 'three',
        ),
        '#ajax' => array(
            'callback' => 'ajax_example_simplest_callback2',
            'wrapper'  => 'replace_textfield_div',
        ),
    );

    $number = (!empty($form_state['values']['changethis']) ? $form_state['values']['changethis'] : t("Not changed yet"));
    $form['replace_textfield'] = array(
        '#type'        => 'textfield',
        '#title'       => t("The default value will be changed"),

        // Not that value was changed in the description
        '#description' => t("Say something about why you chose") . " '" . $number . "'",

        // Also add next line if you want to see it in the text box
        '#value'       => $number,

        '#prefix'      => '<div id="replace_textfield_div">',
        '#suffix'      => '</div>',
    );

    return $form;
}

function ajax_example_simplest_callback2($form, $form_state) {
    return $form['replace_textfield'];
}