如何使用drupal7中的自定义表单更新字段

时间:2016-12-05 14:56:31

标签: php forms function drupal-7

我有三个领域:名字,姓氏和出生年份。在代码中,当我按下提交按钮时,我很容易插入到数据库中,所以当我想单击“编辑”按钮时,我想更新这些字段。请任何人帮我编写此更新代码,将插入的数据放入数据库中,或者请发布代码。

<?php
function my_module_menu() {

  $items = array();
  $items['my_module/form'] = array(
    'title' => 'My form',
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_my_form'),
    'access arguments' => array('access content'),
    'description' => 'My form',
    'type' => MENU_CALLBACK,
  );
  $items['my_module/edit'] = array(
    'title' => t('Edit Name'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array('my_module_edit'),
    'access callback' => TRUE,
    'type' => MENU_CALLBACK,
  );
return $items;
}

function my_module_my_form($form_state) {
  $form['first_name'] = array(
  '#type' => 'textfield',
  '#title' => t('First name'),
  '#required' => TRUE, // Added
  );
 $form['last_name'] = array(
  '#type' => 'textfield',
  '#title' => t('Last name'),
  '#required' => TRUE, // Added
 );
  $form['year_of_birth'] = array(
  '#type' => 'textfield',
  '#title' => ('Year of birth'),
  '#description' => 'Format is "YYYY"',
 ); 

  // Adds a simple submit button that refreshes the form and clears its contents -- this is the default behavior for forms.
 $form['submit'] = array(
   '#type' => 'submit',
   '#value' => 'Submit',
 );

function  my_module_edit($form, &$form_submit){

$form['Edit'] = array(
   '#type' => 'Edit',
   '#value' => 'Edit',
 );  

return $form; 

}
function my_module_my_form_submit($form, &$form_state){
  $firstName = $form_state['values']['fname'];
  $lastName=$form_state['value']['lname'];
  $yearofbirth = $form_state['values']['yearOfbirth'];


$query ="INSERT INTO `slideshow`.`mymoduledb`(`first_name`,`last_name`,`year_of_birth`) VALUES ('{$firstName}','{$lastName}','{$yearofbirth}')";
  //$result=db_query($query);

  if ($success = db_query($query)) {
    // Tell the user that the employee has been saved.
    drupal_set_message(t(' has been saved.'));
  }else{ // If there's an error, $success will evaluate to FALSE, and the following code will execute.
    drupal_set_message(t('There was an error saving your data. Please try again.'));
      }
   }
}

1 个答案:

答案 0 :(得分:0)

为了编辑表单字段,您可以重复使用表单并设置默认值字段'#default_value' 使用相应的唯一ID来存储从数据库获取的表单提交数据。请参阅示例代码的链接。

https://www.drupal.org/node/2069383