Tôi gặp sự cố khi tạo mô-đun Drupal. Tôi đã tạo một biểu mẫu để thêm vào cơ sở dữ liệu nhưng tôi không có may mắn với việc tạo biểu mẫu để chỉnh sửa một số bản ghi ở đây là vấn đề của tôi. Vấn đề là khi tôi tải các giá trị vào tải biểu mẫu từ cơ sở dữ liệu và thay đổi chúng rồi sau đó nhấp vào làm mới biểu mẫu gửi trước khi gửi giá trị mới. Vì vậy, nó cập nhật vào cơ sở dữ liệu cùng một điều như nó được. Dưới đây là một mã số:Chỉnh sửa biểu mẫu trong mô-đun drupal?
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;
}
Và đây là một mẫu mã nộp:
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'],
)));
}
Nếu ai đó có cùng một vấn đề hoặc biết làm thế nào để giải quyết vấn đề này, xin vui lòng giúp tôi.
Xin cảm ơn trước.
Bạn có thể chỉnh sửa mã và xóa phần bổ sung không? Như D34dman đã chỉ ra, mã định nghĩa cùng một hàm hai lần. – kiamlaluno