Use $_POST after submit form

Solutions

I use this code in form submit:

$_SESSION['Tel_number'] = $form_state->getValue('Tel_number');

And use this code in controller:

$TelNumber = array(
   '#markup' => $_SESSION['Tel_number'],
);
return $TelNumber ;

Your controller is in a new request. If you stored the data in the database retrieve it from there. If you did not store the data in submit in some place it is no longer available. You could use a session variable or a url query parameter to transfer the data from the form submit to the controller:

public function myFormSubmit($form, FormStateInterface $form_state) {
  ...
  $form_state->setRedirect('route_to_controller', [], ['query' => [
    'Tel_number' => '123456789',
  ]]);
}

and the controller

public function showNumber(Request $request) {

  $build['test'] = ['#markup' => $request->query->get('Tel_number')];

  return $build;
}
Tags: Drupal 8 / Forms

Similar questions

D8 | submitForm | Get the $_POST - vars via $form_state or via \Drupal::request()?
On an regular sumbmitForm function, should I fetch the user input via \Drupal::request()->request->get... or is $form_state->getValues(); the better (more saver) way? thx
$_POST undefined from remote server POST
I am writing a Drupal 7 module which is listening for HTTP POST messages to be sent by a 3rd party remote application. For testing I am sending messages using the Firefox Poster extension. If I POST the message, the following code fails to place any value in my local vars (I get 'undefined index'): However, if I send the message using GET, the vars...
Passing $_POST data from page to page in Drupal
Does anyone know how to transfer $_POST data (HTML form data) from one page to another in Drupal? I have two PHP pages I'm including in two different Drupal nodes, but I can't pass POST data between them. Is there an easy way to do this?
drupal_mail - Need to Remove $_POST
I am new to the drupal_mail function and need help. The code I have works, but I want to get rid of the $_POST parameter to limit my security holes. I don't know how to get the posted variables without using the $_POST parameter, and ideally, I would like to single each parameter out to put a custom label on it. Any help would be greatly appreciate...
Drupal 7 - undefined variable _POST error in export_ui
Our Drupal 7 site has suddenly begun generating this error warning: Undefined variable: _POST in require_once() (line 1 of /home/website/public_html/sites/all/modules/ctools/plugins/export_ui/ctools_export_ui.class.php). We hadn't made any changes or updates prior to this happening, which is why it is puzzling, if not worrying. What is also puzzlin...
Upload file does not show up in $_POST or $_FILES, only $HTTP_RAW_POST_DATA
I am trying to add an async upload to my Drupal 6 custom module. The problem is, though, that when the upload occurs (I have a break point on my php function) no data can be seen in $_POST or $_FILES. All the data for the file gets put into $HTTP_RAW_POST_DATA. I would expect it to be in the $_FILES array. Can anyone tell me what I'm doing wrong. O...

Also ask

We use cookies to deliver the best possible experience on our website. By continuing to use this site, accepting or closing this box, you consent to our use of cookies. To learn more, visit our privacy policy.