Issues with POST method in PHP

后端 未结 4 1623
失恋的感觉
失恋的感觉 2021-01-21 04:46

The Issue:

Undefined POST variables after form submission.

Research and Troubleshooting Done:

  • Read over a multit
4条回答
  •  青春惊慌失措
    2021-01-21 05:00

    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

    tag, but would love a link to it!

    3] and 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?

提交回复
热议问题