开发者

Insert input into a drupal form programmatically

How can i populate the input field programmatically in drupal form API? Let me illustrate, consider the following example

function form(){
return drupal_g开发者_如何学Pythonet_form('myform');
}
function myform($form_state){
$form['name'] = array(
'#type' => 'textfield',
'$title' => 'Name: ',
 );
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Save',
 );
 return $form;
}
function myform_submit($form,&$form_state){
$form_id = 'myform';
$form_state['values']['name'] = 'Hello World';
drupal_execute($form_id,$form_state);
}

Here, when submit event occurs, instead of getting the values populated, i get the white blank screen of death on the screen. Is something wrong with my syntax??


This is the proper way to use drupal_execute. However, there are other problems with the way you're handling that form: try attaching a submit and validate function to the form. When you do that, you will also need to move the drupal_execute out of the submit function (which may be causing the problem you're having now). If you need to save data, you need to use the DB abstraction layer, drupal_write_record, or one of the existing hooks.

$form_state = array(
  'submitted' => true,
  'values' => array(
    'name' => 'Hello World',
    'op' => t('Save'),
    'submit' => t('Save'),
    'form_id' => 'myform',
  ),
);

drupal_execute('myform', $form_state);
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜