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

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.

share|improve this question

1 Answer

up vote 3 down vote accepted

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.

share|improve this answer
Thanks yvan, that worked though i had to echo a render array so i added: echo render($return); – silkAdmin Aug 25 '11 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 Jan 26 '12 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. – Charlie S Apr 4 '12 at 22:29

protected by Community Oct 20 '11 at 12:51

This question is protected to prevent "thanks!", "me too!", or spam answers by new users. To answer it, you must have earned at least 10 reputation on this site.

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