I have a node form with some fields, now I want to append some more fields to it on an ajax callback. in the ajax page I have rendered the new form elements with drupal_render function
My problem is that I am not getting the newly built form fields in POST or form_state. Shall I use AHAH or can I continue with this ajax calls?

$form['country'] = array( 
            '#title'    =>t('Ship To'), 
            '#required' => true ,
            '#options' => $options,
            '#weight' =>10,
            '#attributes' =>  array('id' => $cn_var),
        $form['dyn_cost'] = array( 
            '#title'    =>t('Shipping Cost'),   
            '#required' => true ,
            '#size' =>25,
            '#weight' =>11,
            '#id' =>  $co_var,

        print drupal_render($form);

This is my ajax form and it is supposed to be appending to the node form.

  • 1
    May be you want to check the AJAX examples in the example module. – BetaRide Mar 28 '12 at 6:13
  • Is there any problem with my current code ? – niksmac Mar 28 '12 at 6:15
  • 5
    Use AHAH. By printing and exiting you're not altering the $form that Drupal knows, and it won't accept data that it's not expecting. In D6 it's not as fun as #ajax in D7, though. Check out an answer I just gave that's very similar, although its syntax applies to D7: drupal.stackexchange.com/questions/59224/… – Charlie Schliesser Jan 31 '13 at 20:34
  • @CharlieS why not to post it as an answer? NikhilM are you still interested in this at all? – Mołot Dec 30 '13 at 15:27

You can use Conditional Fields module to display form fields with certain conditions. No need to use custom ajax calls.

Conditional Fields allows you to manage sets of dependencies between fields. When a field is “dependent”, it will only be available for editing and displayed if the state of the “dependee” field matches the right condition.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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