Grabbing $_POST data


This wasn't a Drupal problem at all.

My problem was that I was getting an empty $_POST array when I tried to process a naive HTML form with naive PHP. I was blaming this on Drupal because I'm unfamiliar with it but know enough about it to know that it changes the way $_SESSION variables are to be handled (as arrays) and that we are on a pretty non-standard setup (as mentioned in the question).

The actual cause of the problem was something else entirely. It was a series of warnings in my Apache error logs that gave it away.

Part of what I was doing to sanitize the input from users was to use mysql_real_escape_string() on input. I didn't realize that this actually required an active connection to a mysql db. What was happening was that the Apache user under whom the php was executed did not have valid credentials to the db so the function was returning FALSE when it was unable to connect.

Remedying this situation caused $_POST to work as expected. It also explains why I couldn't find any documentation on how Drupal breaks $_POST: it doesn't break it.

This answer isn't strictly related to Drupal but I've decided to include it after all so that any poor schmuck who finds himself going down the same dead end I was following can save himself a few hours of googling.

Further, mysql_real_escape_string is deprecated as of PHP 5.5.0 so you (I) should probably be doing this better anyway. And use Drupal's provided form API instead of rolling your own.

To tackle your exact question, let me first tell you that what happens in the form is the correct thing.

When you submit the form, it takes data, validates it and calls appropriate submit handlers to take care if the rest. The form, once its routing is completed, does a 302 redirect to the same location so a browser F5 hit does not resubmit data. It's a general practice to avoid double submissions.

You will need to make a module that alters the form using hook_form_alter and alter the form.

You can change form's action URL to the other PHP file or add an extra submit handler that posts data to the other raw php file using drupal_http_request.

You will find sanitized and structures data in $form_state['values'] (second param of a submit handler is usually called $form_state). $_POST is also available at this stage.

Tags: Forms

Similar questions

How to get $_POST and $_GET parameters
I ajaxly call a route and pass data to it, In my controller, I want to get $_POST['var'] , $_POST['var2'], it seems because of using HttpFoundation in drupal 8, $_POST and $_GET are not exists in old method, How I can access $_POST parameters in custom Controller?
Getting $_POST empty in AJAX callback
I just want to send address information to drupal for check content existence through the AJAX. Request is sending successfully and callback function also executing. but I am not getting any data in $_POST array. here is my AJAX code. This is callback routing & function At where I am making mistake? is there any other way? Or is there any probl...
Use $_POST after submit form
I create a form and a page with controller in Drupal 8. I want print value of my example input after submit form. For example my input name is "Tel_number". My code in controller page is: But no resault print.
using $_POST values in form alter to add validation
it's not the first time that i get into trouble using $_post in a node form alter, this time i'd like some assistance. basically, i need to pass some parameters to a form (and i should not pass the appended to the url, as query parameters). but $_POST values don't work as expected (they work until reaching validation phase). So.. in a page i pass a...
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
Drupal form ahah element, problem with empty $_POST
I'm making a module for ubercart. It's nearly finished, but now I'm working on the admin page. I want the admin to be able to add options, like the poll module adds choices. The problem I have is that the admin form is generated through drupal_get_form, which leaves the $_POST empty. $_POST['form_build_id'] seems to be needed to get the form from c...

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.