3

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.

Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
Saphiric
  • 81
  • 1
  • 8
  • Side note: make sure you're using PDO instead of `mysql_query()` http://wiki.hashphp.org/PDO_Tutorial_for_MySQL_Developers – Tek May 29 '16 at 06:43
  • @Orions the request method check does indeed come back POST after adding only that check and doing nothing else. – Saphiric May 29 '16 at 12:41

4 Answers4

3

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.

Community
  • 1
  • 1
Ajeet Shah
  • 18,551
  • 8
  • 57
  • 87
  • Unfortunately switching `` with the above didn't work =( – Saphiric May 29 '16 at 12:44
  • It does indeed output 1. `var_dump($_POST)` comes back an empty array still though. Even with the above and below solutions. – Saphiric May 29 '16 at 12:57
  • just Bootstrap. A lot of the posts regarding missing name tags causing this problem were using it and they all said that once they added name tags it worked. I'll check on that front as well. Right now I'm looking into why a successful POST may not actually contain any data. It's coming up with a bunch of AJAX, JQUERY and PHP solutions. I may try one of these like @user285743 mentioned. – Saphiric May 29 '16 at 13:15
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/113254/discussion-between-orions-and-saphiric). – Ajeet Shah May 29 '16 at 13:30
1

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)

Manish Dhruw
  • 753
  • 9
  • 18
  • Unfortunately this produced the same result. It seems as though the `$_SERVER('REQUEST_METHOD')` is coming back as POST. However the actual data is not coming over properly. I'm looking into that now. – Saphiric May 29 '16 at 13:13
0

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

Deepak Rai
  • 52
  • 1
  • 2
0

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?

Davis
  • 856
  • 4
  • 11
  • The `$_SERVER['REQUEST_METHOD']` reported back that it was indeed a POST method. `var_dum($_POST)` came back with an empty array. I'm curious about whether or not it could be similar to your redirect issue. What was happening with that? – Saphiric May 29 '16 at 12:54
  • @Saphiric: see my edited answer for information on my particular redirect issue(s) and a few other things to try debugging. – Davis May 29 '16 at 15:31