Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Issues with POST method in PHP

The Issue:

Undefined POST variables after form submission.

Research and Troubleshooting Done:

  • Read over a multitude of questions here, almost all had to do with not having a name tag on the form field. All of my fields have a tag and ID present.
  • Configured my PHP.ini to have $HTTP_RAW_POST_DATA set to -1
  • Followed tutorials across PHP.net and W3SChools

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 enter image description here

Output After Submission: enter image description here

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.

like image 249
Saphiric Avatar asked May 13 '26 23:05

Saphiric


2 Answers

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.

Hence, the best solution would be to configure your PHPStorm to use your local web server, XAMPP in your case, for your current project. Follow the below steps for same:

  1. Configure your local server's virtual hosts with current project directory as root directory
  2. Make sure you can access the project files after your configuration in the above step. For example: http://localhost/signup.html should be accessible via browser.
  3. Now, in PHPStorm, go to Run -> Edit Configuration and select each file and configure the url for each file as show in below image. E.g. for signup.html file use http://localhost/signup.html.
    • You do not need to set url (Step 3) for all files in your project. Just do it for the default (starting) file - e.g. index.html or login.html or signup.html. All other files linked with <a> or action="register.php" in forms will automatically be served with the local server.

Run - Edit Configuration - Screenshot

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.


But if your issue is caused by some other reasons, please consider checking below points:

  • The form fields should have name attribute, e.g. "name='first_name"` in signup.html file.
  • echo $_SERVER["REQUEST_METHOD"] == "POST" prints 1 when written in register.php file which means that your form is being submitted successfully.
  • The value 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.
  • Check if echo file_get_contents("php://input"); displays data in format: first_name=John&last_name=Doe ..... Refer php.net.
  • Check if var_dump($_POST) displays form data in an array.

Hopefully, it helps you. Feel free to comment.

like image 164
Ajeet Shah Avatar answered May 15 '26 12:05

Ajeet Shah


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)

like image 23
Manish Dhruw Avatar answered May 15 '26 14:05

Manish Dhruw



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!