Community Documentation

Getting a form element without a form

Last updated May 17, 2009. Created by Amazon on February 2, 2006.
Edited by bekasu, add1sun. Log in to edit this page.

I'm using AJAX to replace form elements within a form and I need to get a form element, such as a select box, without the surrounding form. Here's how!

Build your $form like normal, then call this:

<?php
print form_render(form_builder('my_select', $form));
?>

Correction for Drupal 5

Note that you should use drupal_render() for Drupal 5, NOT form_render().

When outputting as JavaScript code, you could do something like this:

<?php
$form_item
= $form['some_form_item'];
$js_form_item = drupal_to_js(drupal_render($form_item));
$js_code = "var some_form_item = $js_form_item;";
?>

Archive

Drupal’s online documentation is © 2000-2012 by the individual contributors and can be used in accordance with the Creative Commons License, Attribution-ShareAlike 2.0. PHP code is distributed under the GNU General Public License.