The Issue:
Undefined POST variables after form submission.
Research and Troubleshooting Done:
At this point I'm lost. The data simply refuses to post, it all comes back undefined. Below is the HTML, PHP, and two screenshots showing the issue.
I am using PHPStorm's built in server on Windows.
signup.html
<div class="text-center col-md-4 col-md-offset-4">
<form id="user_signup" class="form-horizontal signInFields" action="../php/register.php" method="POST">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
register.php
// Variables from the sign-up form POST action
$first_name = $_POST["first_name"];
$last_name = $_POST["last_name"];
$user_email = $_POST["user_email"];
$user_id = $_POST["user_id"];
$user_password = $_POST["user_password"];
$confirm_password = $_POST["confirm_password"];
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert($first_name,$last_name,$user_email,$user_id,$user_password);
if($conn->query($testQuery) === TRUE){
echo "New Record Created Successfully!";
} else {
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
Form With Fields Filled In

Output After Submission:

At this point I'm baffled. As far as I can tell from W3Schools, PHP.net, and various questions here I've got it setup properly. However something is clearly off. Any and all help would be greatly appreciated.
Edit:
You are using PHPStorm's built-in web server, which has some issues right now especially with POST requests, e.g. WEB-17317. You can also refer to this answer in this respect.
And that might be the reason you were unable to process your form.
http://localhost/signup.html should be accessible via browser.http://localhost/signup.html.
<a> or action="register.php" in forms will automatically be served with the local server.
Now, you can use the green button in PHPStorm to run your HTML/PHP files and it will open these files in your browser with the url you configured in Step 1. And you should be able to process your forms without any issue.
echo $_SERVER["REQUEST_METHOD"] == "POST" prints 1 when written in register.php file which means that your form is being submitted successfully.post_max_size in your php.ini is set in #M format, e.g. 10M, any other format like 10MB is invalid. Refer php.net.echo file_get_contents("php://input"); displays data in format: first_name=John&last_name=Doe ..... Refer php.net.var_dump($_POST) displays form data in an array.Hopefully, it helps you. Feel free to comment.
Try This
register.php
<?php
$errors = [];
$fields =
[
'first_name',
'last_name',
'user_email',
'user_id',
'user_password',
'confirm_password',
];
$data = [];
if( !empty( $_POST ) )
{
foreach ($fields as $field)
{
if( isset( $_POST[$field] ) )
{
$data[$field] = $_POST[$field];
}
else
{
//empty field handling
$errors[$field] = $field.' required!';
}
}
}
if( empty($errors) )
{
// Add a new user to the database
$conn = new mysqli($servername, $username, $password);
$testQuery = mysql_insert
(
$data['first_name'],
$data['last_name'],
$data['user_email'],
$data['user_id'],
$data['user_password']
);
if($conn->query($testQuery) === TRUE)
{
echo "New Record Created Successfully!";
}
else
{
echo "Error: " . $testQuery . "<br>" . $conn->error;
}
$conn->close();
}
signup.php
<?php require('path/to/register.php'); ?>
<form id="user_signup" class="form-horizontal signInFields" action="" method="post">
<input type="text" id="first_name" name="first_name" placeholder="First Name">
<input type="text" id="last_name" name="last_name" placeholder="Last Name">
<input type="email" id="user_email" name="user_email" placeholder="Email">
<input type="text" id="user_id" name="user_id" placeholder="User ID">
<input type="password" id="user_password" name="user_password" placeholder="Password">
<input type="password" id="confirm_password" name="confirm_password" placeholder="Confirm Password">
<button id="btn_signup" type="submit" name="signup_button">Sign Me Up!</button>
</form>
SIDE NOTE ( If You Are Newbie )
Use method="post" instead of method="POST" (just standards)
You are not validating input fields (dangerous)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With