2012-12-09 25 views
5

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.

+0

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

Trả lời

3

Trước hết, tôi muốn chỉ ra mã ví dụ của bạn đã được dán sai. Tôi thấy hai khai báo cùng một hàm edit_form.

Giả định tuyên bố đầu tiên là dán sai và tiếp tục trả lời câu hỏi này.

Vấn đề chính tôi đã thấy trong tuyên bố biểu mẫu của bạn là bạn đang sử dụng "#value" để lưu trữ giá trị mặc định. Vui lòng sử dụng "#default_value".

Nếu bạn sử dụng #value, nó bỏ qua giá trị do người dùng gửi.

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

Ví dụ thay đổi,

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

để

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

Ngoài ra tôi khuyên bạn kiểm tra this link mà là một mô-đun cung cấp rất nhiều ví dụ để tương tác với Drupal .

+0

Cảm ơn rất nhiều D34dman. Nhưng bây giờ tôi có vấn đề với default_value này mang lại cho tôi thông báo: Một sự lựa chọn bất hợp pháp đã được phát hiện. Vui lòng liên hệ với quản lý. –

+0

bạn đang sử dụng ajax? – D34dman

+0

Tôi đã giải quyết được vấn đề của mình. Cảm ơn nhiều. –

Các vấn đề liên quan