2

I have created a content type "Type A" and added three fields to content type. I want to display value in third field equal to (value of first field - value of second field).

I want to implement the same using ajax functionality. I have created a custom module and alter the "node form" and add ajax to field 1 but ajax is not working on change event of textfield.

Code Snippet:

function employee_login_form_alter(&$form, &$form_state, $form_id){

    $form['field_total_value']['#prefix'] = '<div id="calculate-total-value">';
    $form['field_total_value']['#suffix'] = '</div>';


    $form['field_value_first']['#ajax'] = array(
        'callback' => 'employee_login_ajax',
        'wrapper' => 'calculate-total-value',
        'method' => 'replace',
        'event' => 'change',    
    );

    $form['field_value_first']['und'][0]['#ajax'] = array(
        'callback' => 'employee_login_ajax',
        'wrapper' => 'calculate-total-value',
        'method' => 'replace',  
        'event' => 'change',        
    );

}

function employee_login_ajax(&$form, &$form_state) { //print_r($form); exit;
  print_r($form_state['values']);
}
4
  • Hello. What form elements keeps your first and second fields?
    – Mołot
    Commented Sep 11, 2013 at 13:02
  • field_value_first and field_value_second are first and second fields. Commented Sep 11, 2013 at 13:11
  • Did you meant $form['field_value_first']? Well, you can dpr($form) with devel to be sure. The rest is in my answer.
    – Mołot
    Commented Sep 11, 2013 at 13:13
  • yes, $form['field_value_first'] and $form['field_value_second'] are two fields. Commented Sep 11, 2013 at 13:26

2 Answers 2

2

You don't need ajax, you can do this with jQuery or javascript. Something like this might work.

First, create an external js file and set up your change events.

jQuery(document).ready(
    function()
    {
        jQuery("#field2").change(subtract_fields);
        jQuery("#field1").change(subtract_fields);

        function subtract_fields()
        {
            if (jQuery("#field1").value != "" && jQuery("#field2").value != "")
            {
                return jQuery("#field1").value - jQuery("#field2").value;
            }

            return "";
        }
    }
);

Next, load the external js file using drupal_add_js.

Clear your cache and voila.

3
  • Good idea, and faster than ajax, but fails if calculations needs something from database. Upvoted anyway.
    – Mołot
    Commented Sep 11, 2013 at 13:01
  • I know that I can do this with jquery or javascript. But, ajax should work which is not working. Commented Sep 11, 2013 at 13:15
  • If you're just subtracting two values that exist on the form, I don't see why you would need ajax. Commented Sep 11, 2013 at 13:16
2

In ajax callback you are not supposed to print form part you want updated. You are supposed to return form part that needs updating. And you can set field's value using $form_state['input'].

function employee_login_ajax(&$form, &$form_state) {
  $form_state['input']['field_total_value'] = $your_value;
  return $form['field_total_value'];
}

Ah, and don't forget <input> usually does not support change event. You can use one of the following instead:

  • keyup
  • blur
  • maybe input, this one depends on browser

Or don't set ['#ajax']['event'] at all, to let Drupal decide.

8
  • Yes, you are right. But issue is that when I enter any value in field_value_first and leave it, ajax does not work on the same. I have add print_r in the ajax callback only to check ajax is working or not. Commented Sep 11, 2013 at 13:13
  • @JayendraKainthola answer updated
    – Mołot
    Commented Sep 11, 2013 at 13:16
  • I added comment before I got your update answer. Commented Sep 11, 2013 at 13:19
  • @JayendraKainthola yea, your comment made me update my answer :)
    – Mołot
    Commented Sep 11, 2013 at 13:20
  • Before, I did not set ['#ajax']['event'] and it was not working for me. Then I tried to set ['#ajax']['event'] and hope that it work. Trying with other option but not working. Commented Sep 11, 2013 at 13:28

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.