1

I'm attaching ajax on textfield value. One enter value in textfield and ajax callback returns form element.

Below code is showing ajax 500 error. Can anyone tell what is mistake in this code?

function hook_form_alter(&$form, \Drupal\Core\Form\FormStateInterface $form_state, $form_id) {

    $form['field_base_price']['widget'][0]['value']['#ajax'] = array(
            'event' => 'change',
            'wrapper' => 'final-price-wrapper',
            'callback' => 'final_price_ajax_callback',
            'method' => 'replace',
          );

    $form['field_final_price']['#prefix'] = '<div id="final-price-wrapper">';
        $form['field_final_price']['#suffix'] = '</div>';
          $form['final_price_label'] = array(
            '#weight' => 11,
            '#prefix' => '<div id="final-price-wrapper" class="field-name-field-final-price form-item">',
            '#suffix' => '</div>',
          );
          $form['final_price_label']['title'] = array(
            '#type' => 'markup',
            '#markup' => '<label>' . $form['field_final_price']['widget'][0]['value']['#title'] . '</label>',
          );

    }

    /**
     * Final price AJAX callback.
     */
    function final_price_ajax_callback($form, $form_state) {
      return $form['final_price_label'];
    }

enter image description here

closed as too broad by Neograph734, Shawn Conn, Pierre.Vriens, mradcliffe, DRUPWAY Sep 11 '17 at 6:13

Please edit the question to limit it to a specific problem with enough detail to identify an adequate answer. Avoid asking multiple distinct questions at once. See the How to Ask page for help clarifying this question. If this question can be reworded to fit the rules in the help center, please edit the question.

  • A 500 error can mean anything, could be a missing ";". You need a full error message, so that you know where to look. – 4k4 Sep 8 '17 at 6:36
  • Do you have access to the apache or nginx error log? As 4k4 mentioned, more detail is needed to provide the proper feedback. – JurgenR Sep 8 '17 at 7:36
1

You can use simple jQuery to achieve this then. Try putting the following code in your JS file:

$("#fname").blur(function(){
  var fname = $("#fname").val();
$("#lname").val(fname);
});
  • but on saving my node, this value not saved in db, we have to go with the ajax approach because jquery is client side. – Sugandh Khanna Sep 12 '17 at 6:05
  • You can get the data of field on the controller.php file and then assign the data to some variable. Then assign that variable to the column variable in the DB. Insert the values in the DB. Done. – Pranay Agarwal Sep 12 '17 at 9:12
0
  1. You need to include double-colon(::) operator before callback.
  2. Use proper arguments in the ajax callback (array &$form, FormStateInterface $form_state).

A working sample code,something close to yours,

     /**
     * {@inheritdoc}
     */
    public function buildForm(array $form, FormStateInterface $form_state) {



      $form['team_name'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Team Name'),
        '#maxlength' => 64,
        '#size' => 64,
        '#attributes' => [
          'class' => [
            'form-control',
          ],
        ],
        '#required' => TRUE
      ];

      $form['team_name']['#ajax'] = array(
              'event' => 'change',
              'wrapper' => 'final-price-wrapper',
              'callback' => '::final_price_ajax_callback',
              'method' => 'replace',
            );

      $form['team_r'] = [
        '#type' => 'textfield',
        '#title' => $this->t('Team r'),
        '#maxlength' => 64,
        '#size' => 64,
        '#attributes' => [
          'class' => [
            'form-control',
          ],
        ],
        '#required' => TRUE
      ];

        $form['team_r']['#prefix'] = '<div id="final-price-wrapper">';
        $form['team_r']['#suffix'] = '</div>';
        return $form;   


    }
  /**
   * Final price AJAX callback.
   */
  function final_price_ajax_callback(array &$form, FormStateInterface $form_state) {
    $form['some_field'] = [
      '#type' => 'textfield',
      '#title' => $this->t('reer r'),
      '#maxlength' => 64,
      '#size' => 64,
      '#attributes' => [
        'class' => [
          'form-control',
        ],
      ],
      '#required' => TRUE
    ];
    return $form['some_field'];
  }

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