Tell me more ×
Drupal Answers is a question and answer site for Drupal developers and administrators. It's 100% free, no registration required.

I have a content type called article. I have added a field called attribute which can have unlimited values. At present there is an 'Add another term' button present in the add/edit node page to add those extra values. But I want these fields added dynamically and values populated based on the values entered on the body field, i.e. if I had entered #abc, #cdf, #aws on the body field, on blur even, three attribute terms must be generated with values abc, cdf, aws. I have added a javascript code to add the fields, but when saved, only the first value only gets saved.What more should be done or anything is missing? How can this be done. I have done the part of extracting the values. i need to know how form field can be added?

function isevencheck(value) {
    if (value%2 == 0)
        return true;
    else
        return false;
}
    jQuery.noConflict();
    jQuery( document ).ready(function() {
        jQuery('#edit-body-und-0-value').blur(function() {
         var text = jQuery('#edit-body-und-0-value').val().match(/\#(\w)+/g)  ;
        $i=0;
        contents="";
        for (var obj in text) {
            if(isevencheck($i)){
                cls="draggable odd";    
            }else{
                cls="draggable even";
            }
             text =text[obj].replace('#', '') ;
            contents+='<tr class="'+cls+'"><td class="field-multiple-drag"><a href="#" class="tabledrag-handle" title="Drag to re-order" style=""><div class="handle">&nbsp;</div></a></td><td><div class="form-item form-type-textfield form-item-field-attribute-und-'+$i+'-value"><input class="text-full form-text" type="text" id="edit-field-attribute-und-'+$i+'-value" name="field_attribute[und]['+$i+'][value]" value="'+text+'" size="60" maxlength="255"></div></td><td class="delta-order tabledrag-hide" style="display: none;"><div class="form-item form-type-select form-item-field-attribute-und-'+$i+'--weight"><label class="element-invisible" for="edit-field-attribute-und-'+$i+'-weight">Weight for row 1 </label><select class="field_attribute-delta-order form-select" id="edit-field-attribute-und-'+$i+'-weight" name="field_attribute[und]['+$i+'][_weight]"><option value="-1">-1</option><option value="0" selected="selected">0</option><option value="1">1</option></select></div></td> </tr>';$i++;


        }
 jQuery('#field-attribute-values tbody')
  .html(contents);


});
    });

I think the thing that is left to be done in this is to rebuild form using ajax. How that is to be done?

share|improve this question
You can't add new form fields manually on the client-side, the server needs to be updated with the details as well (otherwise you'll receive an "Illegal choice detected" error when saving the form). You can set the value for existing elements, but adding/removing elements is more complicated and requires proper integration with the form API. Have a read of AJAX Forms in Drupal 7, that should give you the info you need – Clive May 10 at 13:46
How the server needs to be updated? – user7282 May 10 at 14:08
Read the link... – Clive May 10 at 14:22

Know someone who can answer? Share a link to this question via email, Google+, Twitter, or Facebook.

Your Answer

 
discard

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

Browse other questions tagged or ask your own question.