1

1) Am i missing something here? it's not replacing the textfield value.
2) Also how do i replace if i have a multiple textfield and multiple button in a form.

<?php
    function test($form, &$form_state) {
      if (isset($form_state['values']['your_name'])) {
        $name_value = $form_state['values']['your_name'];
      }
      else {
        $name_value = "";
      }

      $form = array();
      $form['your_name'] = array(
        '#title' => t('Your or your company name'),
        '#type' => 'textfield',
        '#value' => $name_value,
        '#prefix' => '<div id ="subscribers-name">',
        '#suffix' => '</div>',
      );
      $form['load_settings'] = array(
        '#type' => 'button',
        '#value' => t('Load my settings'),
        '#ajax' => array(
          'wrapper' => 'subscribers-name',
          'callback' => 'simplenews_mod_load_subscriptions_ajax',
          'method' => 'replace',
        )
      );
      return $form;
    }

    function simplenews_mod_load_subscriptions_ajax($form, &$form_state) {
      $form_state['values']['your_name'] = "loaded value";
      return $form['your_name'];
    }

1 Answer 1

0
function test($form, &$form_state) {
  $form['your_name'] = array(
    '#title' => t('Your or your company name'),
    '#type' => 'textfield',
    '#prefix' => '<div id ="subscribers-name">',
    '#suffix' => '</div>',
  );
  if (isset($form_state['values']['your_name'])) {
    $form['your_name']['#value'] = 'if';
  }
  else {
    $form['your_name']['#value'] = 'else';
  }
  $form['load_settings'] = array(
    '#type' => 'button',
    '#value' => t('Load my settings'),
    '#submit' => 'simplenews_mod_load_subscriptions',
    '#ajax' => array(
      'wrapper' => 'subscribers-name',
      'callback' => 'simplenews_mod_load_subscriptions_ajax',
      'method' => 'replace',
    )
  );
  return $form;
}

function simplenews_mod_load_subscriptions_ajax($form, &$form_state) {
  return $form['your_name'];
}

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.