Take the 2-minute tour ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I'm creating a form with hidden field, I change the values of that field with jQuery:

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

    if (empty($form_state[FIELDS_NUM])) {
        $form_state[FIELDS_NUM] = 1;
    }

    // the form name
    $form[FORM_NEW_NAME] = array(
        '#type' => 'textfield',
        '#title' => t('New form name'),
        '#description' => t("automatically will be prefixed by isrh_form_"),
        '#required' => TRUE,
    );

    $form[FORM_SEND_AUTH_EMAIL] = array(
        '#title' => t('Send authentication email'),
        '#description' => t('If check will set the status field to disabled'),
        '#type' => 'checkbox',
        '#default_value' => 1,
    );

    $form[FORM_EMAIL_TYPE] = array(
        '#type' => 'select',
        '#title' => t('Email type'),
        '#options' => $mailTpls,
        '#default_value' => 0,
        '#description' => t('will be added as field:  Email template  (field_XYZ_email_tpl)'),
        '#required' => TRUE,
    );

    // Build the number of name fieldsets indicated by $form_state[FIELDS_NUM]
    for ($i = 1; $i <= $form_state[FIELDS_NUM]; $i++) {

        $isFieldRemoved = isset($form_state['values']['field'][$i][FORM_IS_REMOVED]) ? $form_state['values']['field'][$i][FORM_IS_REMOVED] : 0;
        //dpm($form_state['values']['field'][$i][FORM_IS_REMOVED]);
        if($isFieldRemoved == 1){
            drupal_set_message("removed field");
        }

        $form['field'][$i] = array(
            '#type' => 'fieldset',
            '#title' => t('Field #@num', array('@num' => $i)),
            '#collapsible' => TRUE,
            '#collapsed' => FALSE,
            '#prefix' => '<div class="new-field-wrapper">',
            '#suffix' => '</div>',
        );

        $form['field'][$i][FORM_FIELD_NAME] = array(
            '#type' => 'textfield',
            '#title' => t('Field name'),
            '#description' => "max 20 characters",
            '#size' => 20,
            '#maxlength' => 20,
            '#required' => TRUE,
        );

        $form['field'][$i][FORM_FIELD_TYPE] = array(
            '#type' => 'select',
            '#title' => t('Field type'),
            '#options' => $fieldTypes,
            '#default_value' => 0,
            '#required' => TRUE,
        );

        $form['field'][$i][FORM_FIELD_REQUIRED] = array(
            '#type' => 'checkbox',
            '#title' => t("Required"),
            '#description' => t("Is this field will be mandatory in web service."),
            '#default_value' => 0,
        );

        $form['field'][$i][FORM_IS_REMOVED] = array(
            '#type' => 'hidden',
            '#value' => isset($form_state['values']['field'][$i][FORM_IS_REMOVED]) ? $form_state['values']['field'][$i][FORM_IS_REMOVED] : 0 ,
            '#attributes' => array(
                'class' => array('remove-value'),
            ),
        );
    }
    $form['submit'] = array(
        '#type' => 'submit',
        '#value' => 'Create form',
    );

    // Adds "Add another name" button
    $form['add_name'] = array(
        '#type' => 'submit',
        '#value' => t('Add field'),
        '#submit' => array('mymodule_form_add_name'),
    );

    return $form;
}

function mymodule_form_add_name($form, &$form_state) {
    // Everything in $form_state is persistent, so we'll just use
    // $form_state['add_name']
    $form_state[FIELDS_NUM]++;

    // Setting $form_state['rebuild'] = TRUE causes the form to be rebuilt again.
    $form_state['rebuild'] = TRUE;
}

The jQuery:

jQuery(document).ready(function($) {

    //if ($(".new-field-wrapper").length > 1) {
        $('.new-field-wrapper .fieldset-legend')
            .append('<input type="button" value="remove" class="remove-button" style="margin-left: 10px;"/>');

        $('.remove-button').click(function () {
            var fieldSet = $(this).closest("fieldset");

            var removeField = $(this).closest("fieldset").find('.remove-value');
            var isRemoved = removeField.val();
            //var textBox = $(this).closest("fieldset").find(':text');
            if (isRemoved == 0) { // need to remove
                removeField.val(1);
                $(this).val("restore");
                //textBox.removeClass('required');
            } else {
                removeField.val(0);
                $(this).val("remove");
                //textBox.addClass('required');
            }

            $(this).closest("fieldset").find(".fieldset-title").click();

        });
    //}

});

This code add remove button to each field set lengend and when it pressed the value of the hidden field became 1.
When I submit the form ("add field button") I never get the 1 value (I checked that with 'Inspect element').

What am I doing wrong?

share|improve this question
3  
Next time, please do not post so much code when the relevant parts are only several lines, it helps to keep people who try to answer motivated (and believe me, seeing dozens of line of irrelevant code can be frustrating to read through) –  m1n0 Feb 23 at 13:45

1 Answer 1

up vote 6 down vote accepted

From the Form API Reference:

Note that if you plan to use JavaScript to change your hidden element's value, you will need to use the #default_value property rather than #value.

So you just need to change you line

'#value' => isset($form_state['values']['field'][$i][FORM_IS_REMOVED]) ? $form_state['values']['field'][$i][FORM_IS_REMOVED] : 0 ,

to:

'#default_value' => isset($form_state['values']['field'][$i][FORM_IS_REMOVED]) ? $form_state['values']['field'][$i][FORM_IS_REMOVED] : 0 ,
share|improve this answer
    
Correct and quick answer!!!! –  NickF Feb 23 at 14:52

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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