Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 836 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Drupal Form:want to show previous form value as default_value on page

#1
My Goal is if user is submitting this form with "Product Name" value as "YYY". On submit page should reload but this time "Product Name" should show previous valye as default as in this case "YYY".

Here is my code...

function addnewproduct_page () {
return drupal_get_form('addnewproduct_form',&$form_state);
}

function addnewproduct_form(&$form_state) {
$form = array();


$formproductname['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
);

if (isset($form_state['values']['productname']))
{
$form['productname']['#default_value'] = $form_state['values']['productname'];
}

//a "submit" button
$form['submit'] = array (
'#type' => 'submit',
'#value' => t('Add new Product'),
);
return $form;
}

Reply

#2
I usually solve this by putting the submitted value in the $_SESSION variable in the submit hook. Then the next time the form is loaded, I check the $_SESSION variable for the appropriate key, and put the value into the #default_value slot if there's anything present.
Reply

#3
You can use $form_state['storage'] in your submit handler to hoard values between steps. So add a submit function like so:

function addnewproduct_form_submit ($form, &$form_state) {
// Store values
$form_state['storage']['addnewproduct_productname'] = $form_state['values']['productname'];
// Rebuild the form
$form_state['rebuild'] = TRUE;
}

Then your form builder function would become:

function addnewproduct_form(&$form_state) {

$form = array();

$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',

);

if (isset($form_state['storage']['addnewproduct_productname'])) {
$form['productname']['#default_value'] = $form_state['storage']['addnewproduct_productname'];
}

return $form;
}

Just remember that your form will keep being generated as long as your $form_state['storage'] is stuffed. So you will need to adjust your submit handler and unset($form_state['storage']) when are ready to save values to the database etc.

If your form is more like a filter ie. used for displaying rather than storing info, then you can get away with just

function addnewproduct_form_submit ($form, &$form_state) {
// Rebuild the form
$form_state['rebuild'] = TRUE;
}

When the form rebuilds it will have access to $form_state['values'] from the previous iteration.



Reply

#4
Not sure if this would work for you, but you could try adding the `#default_value` key to the form array

$form['productname'] = array (
'#type' => 'textfield',
'#title' => t('Product Name'),
'#required' => TRUE,
'#size' => '20',
'#default_value' => variable_get('productname', ''),
);

That way if the variable is set it will grab whatever it is, but if not you can use a default value.

Reply

#5
Drupal will do this by default if you include:

$formproductname['#redirect'] = FALSE;

In your $formproductname array.
Reply

#6
I prefer to save all values in one time when we are speaking about complex forms :

foreach ($form_state['values'] as $form_state_key => $form_state_value) {
$form_state['storage'][$form_state_key] = $form_state['values'][$form_state_value];
}
Reply

#7
simply adding rebuilt = true will do the job:

$form_state['rebuild'] = TRUE;

version: Drupal 7
Reply

#8
For anyone looking for an answer here while using webform (which I just struggled through), here's what I ended up doing:

//in hook_form_alter
$form['#submit'][] = custom_booking_form_submit;

function custom_booking_form_submit($form, &$form_state) {
// drupal_set_message("in form submit");
// dpm($form_state, 'form_state');

foreach ($form_state['values']['submitted_tree'] as $form_state_key => $form_state_value) {

$form_state['storage'][$form_state_key] = $form_state_value;
}
}

Note: added the ' as it was lost
Reply

#9
In my case I had a couple of drop-downs. Submitting the form posted back to the same page, where I could filter a view and I wanted to show the previously selected options. On submitting the form I built a query string in the submit hook:

function myform_submit($form, &$form_state) {

$CourseCat = $form_state['values']['coursecat'];

drupal_goto("courses" , array('query' =>
array('cat'=>$CourseCat))
);
}

In the form build hook, all I did was get the current query string and used those as default values, like so:

function myform_form($form, &$form_state) {
$Params = drupal_get_query_parameters ();
$CatTree = taxonomy_get_tree(taxonomy_vocabulary_machine_name_load ('category')->vid);

$Options = array ();
$Options ['all'] = 'Select Category';
foreach ($CatTree as $term) {
$Options [$term->tid] = $term->name;
}
$form['cat'] = array(
'#type' => 'select',
'#default_value' => $Params['cat'],
'#options' => $Options
);

$form['submit'] = array(
'#type' => 'submit',
'#default_value' => 'all',
'#value' => t('Filter'),
);

return $form;
}
Reply



Forum Jump:


Users browsing this thread:
2 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through