2012-12-09 5 views
5

Drupal 모듈을 만드는 데 문제가 있습니다. 데이터베이스에 추가하기위한 양식을 만들었지 만 여기에 일부 레코드를 편집하기 위해 양식을 만드는 데 아무런 운이 필요하지 않습니다. 내 문제입니다. 문제는 데이터베이스에서 폼로드로 값을로드하고 변경 한 다음 새 값을 제출하기 전에 제출 단추 폼 새로 고침을 클릭하는 경우입니다. 따라서 그것은 데이터베이스와 동일한 것으로 업데이트됩니다. 여기에 제출 형태 코드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; 
} 

: 그리고 누군가가 같은 문제가 있거나이 문제를 해결하는 방법을 알고 있다면

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'], 
))); 
} 

이, 저를 도와주세요 여기에 코드입니다. 사전에

감사합니다.

+0

코드를 편집하고 여분의 부분을 제거 할 수 있습니까? D34dman이 지적했듯이이 코드는 동일한 함수를 두 번 정의합니다. – kiamlaluno

답변

3

먼저 예제 코드를 잘못 붙여 넣었습니다. 동일한 함수 edit_form의 두 가지 선언이 있습니다.

첫 번째 선언이 잘못 붙여진 것으로 가정하고 이에 대한 답변을 계속하고 있습니다.

양식 선언에 중요한 문제는 "#value"를 사용하여 기본값을 저장하고 있다는 것입니다. "#default_value"를 사용하십시오.

#value를 사용하면 사용자가 제출 한 값을 무시합니다.

  1. Read more about use of #value. 예를 들어, 변화에 대한

  • Read more about use of #default_value
  • ,

    $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.'), 
    ); 
    

    에 또한 내가 강력히 드루팔과 상호 작용하는 예를 많이 제공하는 모듈이다 this link을 확인하는 것이 좋습니다 .

    +0

    고맙습니다. D34dman. 하지만 지금은 나에게 메시지를 준다 default_value 문제가있다 : 불법적 인 선택이 감지되었습니다. 사이트 관리자에게 문의하십시오. –

    +0

    ajax를 사용하고 있습니까? – D34dman

    +0

    나는 내 문제를 해결했다. 고맙습니다. –

    관련 문제