Drupal 8 pagination not working with $form_state->setRebuild(true)

here is my code. When I click on pag 2, form comes up instead of query result. in text box again i have to enter search term and it shows up page 2. please help

‘MembSNo’, ], [ ‘data’ => ‘MembType’, ], [ ‘data’ => ‘MembState’, ], ]; $ query = db_select(‘memberdb’, ‘memberdb’) ->extend(‘Drupal\Core\Database\Query\PagerSelectExtender’) ->element($ element); $ result = $ query ->fields(‘memberdb’, [ ‘MembSNo’, ‘MembType’, ‘MembState’, ]) ->condition(‘MembSNo’, $ membstate, ‘=’) ->limit($ limit) ->orderBy(‘memberdb.MembState’) ->execute(); $ rows = []; foreach ($ result as $ row) { $ rows[] = [ ‘data’ => (array) $ row, ]; } return [ ‘#theme’ => ‘table’, ‘#header’ => $ header, ‘#rows’ => $ rows, ‘#empty’ => $ this ->t(“There are no IRCS records found in the db”), ]; } /** * Build the simple form. * * @param array $ form * Default form array structure. * @param FormStateInterface $ form_state * Object containing current form state. */ public function buildForm(array $ form, FormStateInterface $ form_state) { /* check if user has submitted form */ if ($ form_state->isSubmitted()) { /* display result if form has been submitted */ /* * $ form_state[‘post’][‘name’] in drupal * is like $ _POST[‘name’] in regular php programming */ $ name = $ form_state->getValue(‘name’); // Example query. $ form[‘pager_table_0’] = $ this ->buildTestTable(0, 5, $ name); // Counter of calls to the current pager. $ query_params = pager_get_query_parameters(); $ pager_calls = isset($ query_params[‘pager_calls’]) ? $ query_params[‘pager_calls’] ? $ query_params[‘pager_calls’] : 0 : 0; $ form[‘l_pager_pager_0’] = [ ‘#markup’ => $ this ->t(‘Pager calls: @pager_calls’, [ ‘@pager_calls’ => $ pager_calls, ]), ]; // Pager. $ form[‘pager_pager_0’] = [ ‘#type’ => ‘pager’, ‘#element’ => 0, ‘#parameters’ => [ ‘pager_calls’ => ++$ pager_calls, ], ‘#pre_render’ => [ ‘Drupal\mymembership\Controller\MymembershipController::showPagerCacheContext’, ], ]; }else{ /* create form elements if not yet submitted */ /* imagine */ $ form[‘name’] = array( ‘#type’ => ‘textfield’, ‘#title’ => ‘What is your name?’, ‘#required’ => true ); $ form[‘submit’] = array( ‘#type’ => ‘submit’, ‘#value’ => ‘Submit’ ); } return $ form; } /** * #pre_render callback for #type => pager that shows the pager cache context. */ public static function showPagerCacheContext(array $ pager) { drupal_set_message(\Drupal::service(‘cache_contexts_manager’) ->convertTokensToKeys([ ‘url.query_args.pagers:’ . $ pager[‘#element’], ]) ->getKeys()[0]); return $ pager; } /** * Getter method for Form ID. * * The form ID is used in implementations of hook_form_alter() to allow other * modules to alter the render array built by this form controller. it must * be unique site wide. It normally starts with the providing module’s name. * * @return string * The unique ID of the form defined by this class. */ public function getFormId() { return ‘membershipsearch’; } function membership_show_path_page() { $ f = drupal_get_form(‘membershipsearch’); return $ f; } /** * Implements form validation. * * The validateForm method is the default method called to validate input on * a form. * * @param array $ form * The render array of the currently built form. * @param FormStateInterface $ form_state * Object describing the current state of the form. */ public function validateForm(array &$ form, FormStateInterface $ form_state) { } /** * Implements a form submit handler. * * The submitForm method is the default method called for any submit elements. * * @param array $ form * The render array of the currently built form. * @param FormStateInterface $ form_state * Object describing the current state of the form. */ public function submitForm(array &$ form, FormStateInterface $ form_state) { /* * This would normally be replaced by code that actually does something * with the title. */ $ name = $ form_state->getValue(‘name’); drupal_set_message(‘Form is submitted.’.$ name) ; $ form_state->setValue(‘name’, $ name); $ form_state->setRebuild(true); } } ?>