简体   繁体   中英

Edit form in drupal module?

I have a problem making Drupal module . I created a form for adding into database but i am having no luck with creating form to edit some record here is my problem. The problem is when i load values into form load from database and change them and then click submit button form refresh before it submit new values. So it updates into database same thing as it was. Here is a code :

function edit_form($form, &$form_state) {

$query = db_select('activity', 'f')
  ->fields('f')
  ->condition('IDA', $_GET['edit']);
$thefile = $query->execute();
$title = "";
$desc = "";
$file = "";
$privacy = "";
    while($record = $thefile->fetchAssoc()) 
    {
        $title = $record['title'];
        $desc = $record['description'];ick submit button form refresh before it submit new values. So it updates into database same thing as it was. Here is a good :

function edit_form($form, &$form_state) {

$query = db_select('activity', 'f') ->fields('f') ->co
        $file = $record['trainingresource'];
        $privacy = $record['privacy'];

    }
  $form['activity'] = array(
    '#type' => 'fieldset',
    '#title' => t('Create a new activity'),
    '#tree' => TRUE,


  );
  $form['activity']['title'] = array(
      '#type' => 'textfield',
    '#title' => t('Title'),
    '#description' => t('Please enter the title here.'),
    '#value' => t($title),
  );
$form['activity']['description'] = array(
   '#type' => 'textarea',
    '#title' => t('Enter Description'),
  '#value' => t($desc),
    '#description' => t('Please put description here.'),

  );
 /* $form['activity']['date'] = array(
   '#type' => 'date',
    '#title' => t('Enter activity date'),

    '#description' => t('Please put activity date in here.'),
  ); */
  $form['activity']['file'] = array(
   '#type' => 'file',
    '#title' => t('Submit activity file'),
'#value' => t($file),
    '#description' => t('Please files in here.'),
  );
  $form['activity']['security'] = array(
'#type' => 'radios',
'#title' => t('Privacy'),
'#value' => t($privacy),
'#options' => array('True'=>t('True'),'False'=>t('False')),
);
  // Description

  $form['hidden'] = array('#type' => 'value', '#value' => 'is_it_here');
  $form['submit'] = array('#type' => 'submit', '#value' => t('Save'));
  return $form;
}

And here is a submit form code:

function edit_form_submit($form, $form_state) {
$idt = $_GET['edit'];
$title = trim($form_state['values']['activity']['title']);
$desc = trim($form_state['values']['activity']['description']);
//$date = trim($form_state['values']['activity']['date']['year']."-".$form_state['values']['activity']['date']['month']."-".$form_state['values']['activity']['date']['day']);
$file = "file";
$privacy = trim($form_state['values']['activity']['security']['#value']);


$nid = db_update('activity') // Table name no longer needs {}
->fields(array(
  'title' => $title,
  'description' => $desc,
  //'date' => $date,
  'trainingresource' => $file,
  'privacy' => $privacy,

))
->condition('IDA', $idt,'=')
->execute();
drupal_set_message($idt);
drupal_set_message("Added into database");
drupal_goto('activity', array('query'=>array(
'activ'=>$_GET['activ'],
)));
}

If someone have the same problem or know how to solve this problem , please help me .

Thanks in advance.

First of all i would like to point out your example code has been pasted wrongly. I see two declaration of same function edit_form.

Am assuming the first declaration was a wrong paste and continuing to answer this.

The major issue i have seeen in your form declaration is that you are using "#value" to store the a default value. Please use "#default_value".

If you use #value, it ignores the user submitted values.

  1. Read more about use of #value .
  2. Read more about use of #default_value

For example change,

$form['activity']['description'] = array(
  '#type' => 'textarea',
  '#title' => t('Enter Description'),
  '#value' => t($desc),
  '#description' => t('Please put description here.'),
);

to

$form['activity']['description'] = array(
  '#type' => 'textarea',
  '#title' => t('Enter Description'),
  '#default_value' => t($desc),
  '#description' => t('Please put description here.'),
);

Also i strongly recommend you to check this link which is a module that provides lots of examples to interact with Drupal.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM