PHP $_Post Array Empty Upon Form Submission

$_POST is empty after form submit

Add names to the input types like

<input type="text" id="inputLastname" name="inputLastname" placeholder="Last name">

Now check the $_POST variable.

PHP $_POST array always empty upon form submission

You need to be using the name attribute, not the ID attribute. If no elements have a name attribute, no data will show up in the array.

    <form action="test.php" method="post">
<input name="example" type="text" />
<input name="submit" type="submit" value="Send" />
</form>

$_POST array empty in php after submission of a form using a formData

First, you call fetch which makes a POST request

When you get a response, you log the response object (which doesn't hold anything all that useful, in particular the body of the response will show up as a readable stream and not a string).

That response does show array(1) { ["mode"]=> string(5) "basic" } Array ( [mode] => basic ) basic though, you can see it using the Network tab of the browser's developer tools.

After logging the response you set location.replace(response["url"]); which makes a GET request (to the same URL) and navigates the browser to it.


The GET request is a different request and does not have the request body from the POST request from it.

Since it is a different request, it gets a different response and now $_POST is an empty array.


If you want to navigate to the response then get rid of the JavaScript. Just make the POST request with the form and let the browser render the resulting page.

If you want to use Ajax to make the request then:

  • Don't immediately navigate away from the page
  • Do something useful with the response (starting by calling response.text() to get the data out of the response body and then perhaps using createElement, appendChild and friends to add the data to the current document)

Either _POST or _FILES is empty when submitting form

It was an issue of directory vs. path.

 // The folder where the images will be stored
$uploadDir = CONTENT_PATH . '/';

Should be corrected to:

 // The folder where the images will be stored
$uploadDir = CONTENT_DIR . '/';

Where the global variables CONTENT_PATH and CONTENT_DIR are:

defined('CONTENT_PATH')
or define('CONTENT_PATH', realpath($_SERVER['HTTP_HOST']) . '/Shape_Search/public_html/img/content');

defined('CONTENT_DIR')
or define('CONTENT_DIR', dirname(dirname(__FILE__)) . '/public_html/img/content');

$_POST is empty, form

your fields require name attribute

   <form method = "POST">
<div class = "formDiv">
<label for="name">name</label>
<input name ="name" class="name" id="name">
</div>
<div class = "button1">
<input type="submit" name="submit" value="submit">
</div>
</form>


Related Topics



Leave a reply



Submit