Skip to content Skip to sidebar Skip to footer

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

Solution 1:

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.

Solution 2:

Try This

register.php

<?php$errors = [];

$fields =
[
    'first_name',
    'last_name',
    'user_email',
    'user_id',
    'user_password',
    'confirm_password',
];

$data = [];


if( !empty( $_POST ) )
{
    foreach ($fieldsas$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

<?phprequire('path/to/register.php'); ?><formid="user_signup"class="form-horizontal signInFields"action=""method="post"><inputtype="text"id="first_name"name="first_name"placeholder="First Name"><inputtype="text"id="last_name"name="last_name"placeholder="Last Name"><inputtype="email"id="user_email"name="user_email"placeholder="Email"><inputtype="text"id="user_id"name="user_id"placeholder="User ID"><inputtype="password"id="user_password"name="user_password"placeholder="Password"><inputtype="password"id="confirm_password"name="confirm_password"placeholder="Confirm Password"><buttonid="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)

Solution 3:

You can use input type button instead of button you are using, In this case you can send data using ajax.

Solution 4:

I copied your form and (part) of your register.php file, and didn't have any problems. The data posted as expected. I struggled with something similar before, and it turned out to be a strange redirect issue, so my POST data was lost by the time it got to my controller. Sadly, I can only offer a few suggestions to debug it.

I did want to mention that once you figure out your POST issue, you will need to look at your database interaction stuff... you create a mysqli object, yet call a function called mysql_insert (a function I've never seen and can't find on php.net, so I'm inclined to think that's not going to do anything). A quick search will lead you to many thorough tutorials and walkthroughs on how to use mysqli, so I'll defer to those rather than trying to explain everything here (since you're still trying to solve the POST issue anyway).

1] You don't have to use PDO; mysqli is fine and completely acceptable as long as you use it correctly (here's some information about both from php.net directly).

2] I've never seen a standard that specifies using lowercase "post" instead of "POST" in the <form> tag, but would love a link to it!

3] <input type="submit" value="text"> and <button type="submit">text</button> are functionally the same, as in, both will post the form no problem. There are some differences between the two (being able to dynamically set the "value" instead of something like the "innerHTML" for text, for instance, but that's another issue.

As to further debugging efforts... In register.php, like Orions mentioned, I'm curious what $_SERVER['REQUEST_METHOD'] reports back. At the beginning of that file, try a die($_SERVER['REQUEST_METHOD']) and see what it says. If it says "POST", you're on the right track. If you do a var_dump($_POST); at the beginning of that file, with a die/exit afterwards, what do you see?


There were 2 instance where a redirect was robbing me of my POST variables, both caused by mod_rewrites, as I recall. One was redirecting requests from mydomain.com to www.mydomain.com, and since the action of my form was explicitly mydomain.com, everything was lost. The other issue was a mod_rewrite that was either appending or stripping trailing / from requests (I don't recall which), but this also had the same basic result. If you have an .htaccess file, check that to make sure nothing nefarious or gotcha-y is going on.

Other things to check:

a] In your php.ini, make sure variables_order has "P" in it somewhere (reference)

b] Also in your php.ini, make sure post_max_size is something reasonable; I doubt this is the problem since you're not uploading a file, but if it was set to something very small, I can imagine it might be an issue

c] If you are posting from an http page to an https page, sometimes that will strip out POST data (this may be server-specific, as I feel like I've seen this work sometimes and not others)

d] Did you get file_get_contents("php://input"); as suggested elsewhere? Did it contain your data (will look something like this: key1=value1&key2=value2) or was it too blank?

e] If you create another form on another page and POST it to another file, does the $_POST array get populated?

Post a Comment for "Issues With Post Method In Php"