1

I'm building a form in a custom module, and I'd like to have that form show as it's default value the value that had been saved to the DB during the previous edit, but I have no idea how to do it.

Here is an example of one of my fields:

$form['savings_ages']['savings_age21'] = array(
'#type' => 'textfield',
'#title' => t('21'),
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $whatgoeshere?,
);

My form is the basis for a calculator, so I've got over 80 fields and only ever expect to have 1 row's worth of information in my table.

I have no idea how to pull that out, so any guidance would be welcome. PLease let me know if I need to provide more detail.

EDIT: I'm still stuck on this, my normal query works when I test in the sql window of PHPmyadmin:

SELECT `factors_age21` FROM `savingfactors` WHERE 1 limit 1

but I can't seem to convert it to the correct format to work with db_query function and echo it out as the default value of my form field. PLus I'll still have to make this query 44 times (I have 44 columns) so if there is a more efficient way, I'd appreciate it.

Second Edit: I'm starting to get a partial result, but it's still wrong.

$sql = "SELECT factors_age21 FROM {savingfactors} WHERE factors_age21 LIMIT 1";
$res = db_query($sql);
$item = db_fetch_object($res);

//later
$form['savings_factors']['factors_age21'] = array(
'#type' => 'textfield',
'#title' => t('21'),
'#size' => 10,
'#maxlength' => 10,
'#default_value' => print $item,
);

It IS producing a result, but it's showing me the number "1", when my correct value is "14" any help is appreciated.

Thanks Stephanie

2
$form['savings_ages']['savings_age21'] = array(
'#type' => 'textfield',
'#title' => t('21'),
'#size' => 10,
'#maxlength' => 10,
'#default_value' => $whatgoeshere?,
);

I guess $form['savings_ages'] is a fieldset. If so, then you can use the following code to get the default value that was saved in database during previous edit.

Explanation

When you save a form, a variable gets created with the id that you use to create form element. In your case variable_get('savings_age21', '') will fetch the previously saved value.

Documentation variable_get

Edit

$form['savings_ages']['savings_age21'] = array(
    '#type' => 'textfield',
    '#title' => t('21'),
    '#size' => 10,
    '#maxlength' => 10,
    '#default_value' => variable_get('savings_age21', ''),
);

return system_settings_form($form);

Sorry, I forgot about system_settings_form($form), this way when you save the form a variable with id of form element will get created, later you can retrieve it using variable_get().

If you do not wish to use system_settings_form($form), then you can use variable_set() to save a variable with any name. You have to do this variable_set() inside the submit handler of the form. Later while displaying the form, use the same logic variable_get() to retrieve the variable.

4
  • Could you please provide a link to the documentation that says that "a variable gets created" somehow automatically? I can't seem to recall anything to that effect. – kekkis Jan 22 '13 at 15:19
  • I just tried this, and it didn't work. I also think that a variable is not automatically created. Thank you for replying! – StephanieF Jan 22 '13 at 18:29
  • With the system_settings_form() call this should work. However, depending on Stephanie's module and its purpose, using variables might not be the way to go. – kekkis Jan 23 '13 at 19:02
  • This is what I'musing, however since I'm using fieldsets, the call to the variable isn't '#default_value' => variable_get('savings_age21', ''), the variables are being storred as an array (which is great), but I don't know how to ping the array to fill in the default value. – StephanieF Feb 11 '13 at 19:03
1

Perhaps something like:

// Query for default value
global $user;
$savings_age21 = db_query("SELECT savings_age21 FROM {my_table} WHERE uid = :uid", array(':uid' => $user->uid))->fetchField();

$form['savings_ages']['savings_age21'] = array(
  '#type' => 'textfield',
  '#title' => t('21'),
  '#size' => 10,
  '#maxlength' => 10,
  '#default_value' => $savings_age21,
);

Or else you could load the row, assuming one row per user:

global $user;
$record = db_query("SELECT * FROM {my_table} WHERE uid = :uid LIMIT 1", array(':uid' => $user->uid))->fetchObject();
// later on ...
$form['savings_ages']['savings_age21'] = array(
  '#type' => 'textfield',
  '#title' => t('21'),
  '#size' => 10,
  '#maxlength' => 10,
  '#default_value' => $record->savings_age21,
);
8
  • your suggestion does make sense, however I'm not savings any user information to my table. My DB has 44 columns with one row of information, ideally that single row would only ever have the most up to date set of entries. (I'm not sure thats the most efficient way to set up a DB, but thats a different post). I have 44 ages 21 - 65 with one column for an ID (not user id). does your suggestion still work? need changes? Thank you for your reply! – StephanieF Jan 22 '13 at 18:34
  • Yes, just query the default values from your db table and can use that – David Thomas Jan 23 '13 at 4:23
  • I'm sorry, I'm not getting it. this is what I have now: $record = db_result(db_query('SELECT savings_age21 FROM {savingrecommendation} WHERE id = 1')); //later $form['savings_ages']['savings_age21'] = array( '#type' => 'textfield', '#title' => t('21'), '#size' => 10, '#maxlength' => 10, '#default_value' => $record->factors_age21, ); no results are being returned, if you have any other suggestions I'd appreciate it. Thanks! – StephanieF Jan 23 '13 at 16:31
  • With db_result the value will be in $record – David Thomas Jan 23 '13 at 19:50
  • how do I get it to show? Right now it doesn't. (I'm in D6) – StephanieF Jan 23 '13 at 20:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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