Drupal Form:want to show previous form value as default_value on page

后端 未结 8 1520
春和景丽
春和景丽 2020-12-31 20:57

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

8条回答
  •  轮回少年
    2020-12-31 21:43

    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;
    }
    

提交回复
热议问题