2

Below I have written code for update but when I change the value in form and press submit button still those old values goes to the database. My question is how to get current edited value of my form after it is displayed?

function upgrade_menu(){
    $items = array();
    $items['upgrade_page'] = array(
    'title' => t('Upgrade'),
    'page callback' => 'drupal_get_form',
    'page arguments' => array(' upgrade_update'),
    'access callback' => TRUE,
    );
    return $items;
}

function upgrade_page($form, &$form_state) {
    if (!isset($form_state['storage']['upgrade_module']['search_criteria'])) {
        return drupal_get_form('upgrade_form');
    }
    $title = $form_state['input']['Name'];
    $query = db_select('form_test', 'n')
    ->condition('name', $title, '=')
    ->fields('n',array('name','Id','age','email','salary'));
    $results = $query->execute();
    $res = $results->fetchAssoc();  
    $form['Name'] = array(
        '#title' => t('Name'),
        '#type' => 'textfield',
        '#value' => $res['name'],
    );  
    $form['Id'] = array(
        '#title' => t('Id'),
        '#type' => 'textfield',
        '#value' => $res['Id'],
    );  
    $form['Age'] = array(
        '#title' => t('Age'),
        '#type' => 'textfield',
        '#value' => $res['age'],
    );
    $form['Emailid'] = array(
        '#title' => t('Emailid'),
        '#type' => 'textfield',
        '#value' => $res['email'],  
    );
    $form['Salary'] = array(
        '#title' => t('salary'),
        '#type' => 'textfield',
        '#value' => $res['salary'],   
    ); 
    $form['submit'] = array(
        '#value' => 'Update',
        '#type' => 'submit',
        '#submit' => array('upgrade_update'),
    );
    return $form;
}

function upgrade_form($form, &$form_state) {
    $form['criteria'] = array(
        '#type' => 'fieldset',
        '#title' => 'Update',
        '#attributes' => array('class' => array('container-inline')),
    );
    $form['criteria']['Name'] = array(
        '#type' => 'textfield',
        '#title' => t('Name'),
        '#value' => (isset($form_state['input']['Name'])) ? $form_state['input']['Name'] : "",
        '#size' => 10,
        '#maxlength' => 20,
    );
    $form['criteria']['submit'] = array(
        '#type' => 'submit',
        '#value' => 'search',
        '#submit' => array('upgrade_submit'),
    );
    return $form;
}

function upgrade_submit($form, &$form_state) {
    $form_state['storage']['upgrade_module']['search_criteria'] = $form_state['values'];
    $form_state['rebuild'] = TRUE;
}

function upgrade_update($form, &$form_submit) {
    $name =  $form_submit['values']['Name'];
    $Id = $form_submit['values']['Id'];
    $age = $form_submit['values']['Age'];
    $email = $form_submit['values']['Emailid'];
    $salary = $form_submit['values']['Salary'];
    drupal_set_message(t('You provided a  name',array('name' => $name)));
    drupal_set_message(t('You provided a  Id',array('Id' => $Id)));
    drupal_set_message(t('You provided a  age',array('age' => $age)));
    drupal_set_message(t('You provided a  email',array('email' => $email)));
    drupal_set_message(t('You provided a salary',array('salary' => $salary)));

    $entry = array(
        'name' => $form_submit['values']['Name'],
        'id' => $form_submit['values']['Id'],
        'age' => $form_submit['values']['Age'],
        'email' => $form_submit['values']['Emailid'],
        'salary' => $form_submit['values']['Salary'],
    );
    $table = 'form_test';
    $return_value = db_update($table)
    ->fields($entry)
    ->condition('email',$email,'=')
    ->execute();   
    print drupal_set_message('Database is successfully Updated..');
    return $return_value;
}

1 Answer 1

1

You're mixing up #value and #default_value. If you want to show information in the form but let the user change it, you need to use #default_value. If you want the information to always be the same, and never let the user change it, use #value.

1
  • This is exactly the reason why the code is not working as expected.
    – apaderno
    Aug 7, 2012 at 14:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.