1

I am trying to load a form via ajax, the PHP callback that send the data to the Ajax request looks like this :

function group_form (){

        $node_type = 'group';
        $form_id = $node_type . '_node_form';     
        $node = new stdClass();
        $node->type = $node_type;
        $node->title = 'helllooo';
        $node->language = LANGUAGE_NONE;
        $node->path = '';
        node_object_prepare($node);
        $return=drupal_get_form($form_id,$node);

    echo render($return);
}

I use to have a problem when using return instead of echo as mentioned in this Q, where the whole page was being sent via Ajax instead of just the form element. However now that i use echo all the script attached to that form do not get reloaded, witch cause problem as my form uses the date module with a jQuery popup calender, and infinite value fields witch need some Drupal JS in order to add multiple values without page reload.

So i wonder if there is a way around that, if not, anyone has anyidea of what script i should reload on the ajax succes callback in order to have my date module popup calendar and the drupal "Add more" function for infinite value field working ?

EDIT: I found that using Drupal.attachBehaviors(jQuery('#LoadedContent')); seems to be helping as it brings me from this enter image description here


to this enter image description here,


though clicking on the "Add another item" Button still reload the page and i end up with this : enter image description here

Thanks a bunch for the help.

1

Have a look at the Ajax Framework. It will make your life way easier. You can probably accomplish everything you're trying to do with out a single line of js.

0

You need to pass the JS settings that are created when the form is rendered:

function group_form (){

  $node_type = 'group';
  $form_id = $node_type . '_node_form';     
  $node = new stdClass();
  $node->type = $node_type
  $node->title = 'helllooo';
  $node->language = LANGUAGE_NONE;
  $node->path = '';
  node_object_prepare($node);
  $form = drupal_get_form($form_id,$node);

  $response = render($form);

  $scripts = drupal_add_js();
  if (!empty($scripts['settings']))
  {
    $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
    $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
    $settings .= ');</script>';
  }

  $response .= $settings;

  echo $response;
  drupal_exit();
}

Though personally I'd put the response in an array. So instead of this:

$response = render($form);

$scripts = drupal_add_js();
if (!empty($scripts['settings']))
{
  $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
  $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
  $settings .= ');</script>';
}

$response .= $settings;

echo $response;
drupal_exit();

I'd use this:

$response['form'] = render($form);

$scripts = drupal_add_js();
if (!empty($scripts['settings']))
{
  $settings = '<script type="text/javascript">jQuery.extend(Drupal.settings, ';
  $settings .= drupal_json_encode(call_user_func_array('array_merge_recursive', $scripts['settings']['data']));
  $settings .= ');</script>';
}

$response['form'] .= $settings;

drupal_json_output($response);
drupal_exit();

Your form will be available at data.form in the ajax.

You will also need to call the following after injecting your form into the page:

Drupal.attachBehaviors();

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.