Ok, so I'm working in Drupal 7 and am making a form that either inserts values in the database or updates existing information in the database. Each row of my table has two entries, 'counter_name' and 'expected'. Expected is the number of applicants each counter is expected to process per half hour.
What I have is:
//Get a list of counters with 'expected' set already.
$expected = get_expected_numbers();
//Get's a list of all counters stored in the db
$counters = get_counter_info('not_set');
$counter_names = array();
$expected_names = array();
// Here, I format both arrays so they can be diffed
foreach ($counters as $key => $value) {
array_push($counter_names, $counters[$key]['counter_name']);
}
foreach ($expected as $key => $value) {
array_push($expected_names, $expected[$key]['counter']);
}
//Diff the two arrays to see what counters are there but do not have expectations set.
$expectations_to_be_set = array_diff($counter_names, $expected_names);
$expectations_to_be_set = array_values($expectations_to_be_set);
//If there are any that aren't set, display them.
if (!empty($expectations_to_be_set)){
foreach ($expectations_to_be_set as $key => $value) {
$form[$expectations_to_be_set[$key]] = array(
'#type' => 'textfield',
'#prefix' => '<div class="expectation_inputs">',
'#suffix' => '</div><div class="clear"></div>',
'#title' => $expectations_to_be_set[$key],
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Submit',
);
}
//If they are all set, display them for editing.
else {
foreach ($expected as $key => $value) {
$form[$expected[$key]['counter']] = array(
'#type' => 'textfield',
'#prefix' => '<div class="expectation_inputs">',
'#suffix' => '</div><div class="clear"></div>',
'#title' => $expected[$key]['counter'],
'#default_value' => $expected[$key]['apps_per_half_hour'],
);
}
$form['submit'] = array(
'#type' => 'submit',
'#value' => 'Update',
);
}
function set_expectations_form_submit($form, &$form_state) {
$values_array = $form_state['values'];
unset($values_array['submit']);
unset($values_array['form_build_id']);
unset($values_array['form_token']);
unset($values_array['form_id']);
unset($values_array['op']);
print_r($form_state['values']); echo '<br />'; print_r($values_array);
if ($form_state['values']['submit'] == 'Submit') {
foreach ($values_array as $key => $value) {
$insert_expected = db_insert('floormap_counter_expected')
->fields(array(
'counter_name' => $key,
'expected' => $value,
))
->execute();
}
}
else {
foreach ($values_array as $key => $value) {
$update_expected = db_update('floormap_counter_expected')
->fields(array(
'expected' => $value,
))
->condition('counter_name', $key, '=')
->execute();
}
}
}
So the problem I'm running into is that, first when showing the initial form for the user to set expectations for the first time, I do not display a #default_value because there isn't one yet. When the user fills the text box and hits submit, the name of the input (the counter name) is stored but with no value. The user's input is lost. If I am on the edit form, where the database values are displayed as #default_value, the default value's are sent, not the user input.
Oh, also just to clarify...the reason that I am using $values_array to submit to the DB instead of just using $form_state['values'] is because I unset everything that isn't a 'counter_name' so that I can run it through a foreach loop. Wasn't sure about just unsetting those from $form_state. But the information doesn't show up when using $form_state either.