Retrieves, populates, and processes a form.

Retrieves, populates, and processes a form.

This function allows you to supply values for form elements and submit a form for processing. Compare to drupal_get_form(), which also builds and processes a form, but does not allow you to supply values.

There is no return value, but you can check to see if there are errors by calling form_get_errors().

// register a new user
$form_state = array();
$form_state['values']['name'] = 'robo-user';
$form_state['values']['mail'] = 'robouser@example.com';
$form_state['values']['pass']['pass1'] = 'password';
$form_state['values']['pass']['pass2'] = 'password';
$form_state['values']['op'] = t('Create new account');
drupal_execute('user_register', $form_state);

// Create a new node
$form_state = array();
module_load_include('inc', 'node', 'node.pages');
$node = array(
  'type' => 'story',
$form_state['values']['title'] = 'My node';
$form_state['values']['body'] = 'This is the body text!';
$form_state['values']['name'] = 'robo-user';
$form_state['values']['op'] = t('Save');
drupal_execute('story_node_form', $form_state, (object) $node);


webchick’s picture

Note that in Drupal 7, this function was renamed drupal_form_submit(), which more accurately describes what it does.

joshmiller’s picture

When you want to execute a form in D6, you may have to include the module using module_load_include().

Also, I found this documentation page very helpful:

Configure Settings using drupal_execute


pahariwalla’s picture

Nasty little bug. Talk about figuring out the hard way ;-(

Since drupal_execute eventually fires off drupal_validate_form, the same applies here.

static cache: cannot call drupal_validate_form on the same form more than once

Thanks to a number of folks, there are patches (see link) and I believe that D7 has also been fixed.

apanag’s picture

After 4hrs of debugging i found your message.

Karlheinz’s picture

If you're using this function to programmatically create a custom node, make sure you use the correct form_id. It should not be your implementation of hook_form().

Instead, you should use the form_id of NODETYPE_node_form, of course substituting NODETYPE for your custom node type.

andremolnar’s picture

From: http://drupal.org/node/178506#comment-895418 by: orangecoat-ciallella

Drupal 6 Example Code

$form_state = array();
module_load_include('inc', 'node', 'node.pages');  // new for Drupal 6
$nodeTmp = array('type' => 'story'); // a variable holding the content type

$form_state['values']['type'] = 'story'; // the type of the node to be created
$form_state['values']['status'] = 1; // set the node's status to Published, or set to 0 for unpublished
$form_state['values']['title'] = 'Some Node Title Here';   // the node's title
$form_state['values']['body'] = 'whatever body you might want, or else, leave blank'; // the body, not required

  // the username of the node's author, must be a valid user with access to create this node.
  // Otherwise, see (http://drupal.org/node/178506#comment-726479) above about using user_load() to create nodes as any user, even when not logged in
$form_state['values']['name'] = 'someusername'; 
$form_state['values']['op'] = t('Save');  // this seems to be a required value

// CCK field examples for CCK in D6
$form_state['values']['field_mydropdown']['value'] = 3;    // a select (dropdown, where 3 is the "option" value saved to the db
$form_state['values']['field_myuserref'][0]['uid']['uid'] = 'someusername';  // a user reference example
$form_state['values']['field_mycheckbox']['value'] = 1;  // a checkbox example
$form_state['values']['field_mynoderef'][0]['nid']['nid'] = '[nid:9]';  // a node reference example. In this case, 9 is referring to node 9

  // call the function to create the node, node_revisions, and CCK values.
  // Replace "story" with the name of your form / content type
$errs = drupal_execute('story_node_form', $form_state, (object) $nodeTmp);

  // if there were any validation errors, drupal_execute will return them, then you have a chance to do something
if (count($errs)) {
  // optional code here
nicholasThompson’s picture

... Drupal Execute doesn't return anything. Errors must be retreived using form_get_errors().

ihsanullahkhan’s picture

How to add taxonomy term to the node. I am using taxonomy heirarchical select in the cck form as made a cck field. So how to add this taxonomy cck hiecarachical select through drupal_execute function. I am using drupal 6.
I tried to add taxonomy term but didnot succeed. All others fields are working good. Thanks in advance for your help.

rol’s picture

tried to solve it through many ways, but not seems there's a solution on forums...

one option is to create node and after use taxonomy_node_save() , but i need this taxonomy field as mandatory,

anyone could solve this ?

thank you in advance

tonytosta’s picture

I find that using the node_export_import_form is a nice way to add nodes to a site programatically.

  //create webform 
    $form_state = array();
    $form_state['values']['code'] = _sponsored_ad_webform_definition();
    $form_state['values']['op'] = t('Import');
    drupal_execute('node_export_import_form', $form_state);

The sponsored_ad_webform_definition() function simply returns the array that the node_export module generates when exporting a node.

function _sponsored_ad_webform_definition() {
  $ret = "
  'nid' => '24238',
  'type' => 'webform',
  'language' => '',
  'uid' => '1',
  'status' => '1',
  'created' => '1316551143',
  'changed' => '1317222784',
  'comment' => '2',
  'promote' => '1',
  'moderate' => '0',
  'sticky' => '0',   ..... ";
  return $ret;