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



我有三个字段:名字,姓氏和出生年份。在代码中,当我按提交按钮时,我很容易地将其插入数据库,因此我想在要单击编辑按钮时更新这些字段。请任何人帮助我编码此更新,以将插入的数据放在数据库中或邮政编码。

<?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.'));
      }
   }
}

在编辑表单字段中您可以重复使用表单并设置默认值字段 '#default_value'使用各自的唯一唯一唯一的ID来存储从数据库中获取的表单。引用示例代码的链接。

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

最新更新