I'm having a little problem with my drupal 7 module.
I'm trying to get the $form_state['values']
from $form['base']['st-volume-pricing']['price']
and $form['base']['st-volume-pricing']['till-amount']
in my ajax callback so that I can add them to a results table.
Is this the right way to do it or am I missing something?
function mymodule_form_alter($form, &$form_state) {
$form['base']['st-volume-pricing']['price'] = array(
'#type' => 'uc_price',
'#title' => t('Price'),
'#description' => t('Set the price for this volume\'s reduction'),
'#size' => 10,
'#prefix' => '<table id="volume-pricing-standard-input"><tr><td>',
'#suffix' => '</td>',
);
$form['base']['st-volume-pricing']['till-amount'] = array(
'#type' => 'textfield',
'#title' => t('Till Amount'),
'#description' => t('The amount till which the volume discount applies'),
'#size' => 10,
'#prefix' => '<td>',
'#suffix' => '</td>',
);
$form['base']['st-volume-pricing']['add'] = array(
'#type' => 'submit',
'#value' => t('Add'),
'#submit' => array('mymodule_add_volume_standard'),
'#ajax' => array(
'callback' => 'mymodule_add_volume_standard_ajax_callback',
'wrapper' => 'st-volume-results',
'method' => 'append',
'effect' => 'fade',
),
'#prefix' => '<td>',
'#suffix' => '</td></tr>',
'#title' => t('Click this button to add a standard volume price'),
);
//this is our results table
$form['base']['st-volume-pricing']['st-volume-results'] = array(
'#type' => 'markup',
'#markup' => '<table id="st-volume-results"><thead><tr><th>' . t('Price') . '</th><th>' . t('From Amount') . '</th><th>' . t('Till Amount') . '</th></tr></thead></table>',
);
variable_set('vol-st-price', NULL)
variable_set('vol-st-from', 1)
variable_set('vol-st-till', NULL)
//this is our row that will be inserted
$form['base']['st-volume-pricing']['st-volume-results']['row'] = array(
'#type' => 'markup',
'#markup' => '<tr><td>' . variable_get('vol-st-price') . '</td><td>' . variable_get('vol-st-from') . '</td><td>' . variable_get('vol-st-till') . '</td></tr>',
);
}
When I'm using it inside here:
function mymodule_add_volume_standard_ajax_callback($form, &$form_state) {
//check if there is a previous volume price and set the from field
if (variable_get('vol-st-till') !== NULL) {
variable_set('vol-st-from') = variable_get('vol-st-till') + 1;
};
//add or overwrite the value entered in price
variable_set('vol-st-price') = $form_state['values']['base']['st-volume-pricing']['price'];
//add or overwrite the value entered in till
variable_set('vol-st-till') = $form_state['values']['base']['st-volume-pricing']['till-amount'];
//create new row in results table
return $form['base']['st-volume-pricing']['st-volume-results']['row'];
}
drupal_set_message("<pre>" . print_r($form_state['values']) . "</pre>");
displays 1.
I suppose that $form_state['values'] should only work during the submission of a form. How would one go about doing this when using a hook_form_alter() and ajax callbacks?
I also tried displaying drupal_set_message("<pre>" . print_r($form_state['values']) . "</pre>");
in my function mymodule_add_volume_standard
. Which also displayed 1.
Neither does it work when trying it in the hook_form_alter() which is default behaviour I suppose?
I'd really appreciate it if someone could clarify some things for me.