3

I am trying to load a form via ajax, here is the code i use:

JS:

bigPopupBut.click(function () {
        jQuery.ajax({
            url: "http://site/group_create",
            dataType: "html",
            success: function (data) {
                bigPopup.append(data);
            }
        });
})

The group_create is a PHP script that returns me a form, here is the code i used:

$node_type = 'group';
    $form_id = $node_type . '_node_form';     
    $node = new stdClass();
    $node->uid = $user->uid;
    $node->name = $user->name ;
    $node->field_city['und']['0']['value'] = $user->field_city['und']['0']['value'];
    $node->field_country['und']['0']['value'] = $user->field_country['und']['0']['value'];
    $node->type = $node_type;
    $node->language = LANGUAGE_NONE;
    $node->path = '';
    node_object_prepare($node);
    $return=drupal_get_form($form_id,$node);
return $return;

The Problem i have is that the PHP callback doesn't just return me the form, but a whole page, with headers, body, so when i append it with JS i get a duplicate of the whole page in my bigPopup DIV.

What am i doing wrong ? how could i just get the form without the surounding page?

Thanks all for the help.

1 Answer 1

4

Try just to do echo render($return); exit();.

Otherwise, you should change de delivery callback from the path of your page, and I'm pretty sure that exists one one Drupal for Ajax.

4
  • Thanks yvan, that worked though i had to echo a render array so i added: echo render($return);
    – silkAdmin
    Commented Aug 25, 2011 at 11:26
  • Now that you have the rendered form, does submitting it work for you? How do you submit the form when it's created via AJAX?
    – rafaelcr
    Commented Jan 26, 2012 at 23:35
  • Submitting should work fine. You're $_POST'ing the data back to the server, including the form id and form build id, so Drupal should process the form just fine and place you right back at the URL you were at. Commented Apr 4, 2012 at 22:29
  • You should use drupal_exit() instead
    – AlxVallejo
    Commented Nov 26, 2014 at 14:57

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