Can't get $_POST values (php, html) - php

I can't get the $_POST['name'] values sent from an html form on my php file. I've seen lots of similar questions but nothing helped. I have lot's of includes so I believe it's a scope issue, but I can't figure it out.
index.php
print_r($_POST); //Returns nothing, tried other ways too
//lot's of variables being defined
include 'sql_data_handlers.php';
//instantiating some things
sql_data_handlers.php
//some functions retrieving data from sql db and finally:
include($DOCUMENT_ROOT . "page.html");
page.html
//html stuff
<?php
//Some conditions
include($DOCUMENT_ROOT . "comment_form.html");
?>
comment_form.html
<form action="index.php" name="comment_form" id="comment_form" method="post">
<input type="text" name="name" value="Anonymous" required><br>
//lot's of inputs
<input type="submit">
</form>
I used to have action="send_comment.php" but I realized it could be turned into a function so I ctrl+c and adapted send_comments.php to a function on sql_data_handlers.php. The problem is, now I can't get the $_POST values on index.php to use in the function on sql_data_handlers.php (which is included in index.php).
I would use action="my_php_function_from_data_handlers.php($args)" if it was possible, but I guess it isn't. btw, I already tried action="". This may seem pretty messy but this way I only need one .html for the site layout, pages are on the sql and the .php files do all the job.
Complete source of all files (pretty big, still working on it):
http://pastebin.com/2nRuCpNx

Make sure you don't have any kind of redirects, since you're using index.php to save data, you're probably reloading the page to show the updated comments. And if you're reloading, there'll be no data on $_POST to be printed by print_r().

Try changing the value of the input name attribute to something different from "name". Some configurations have issues with that, especially wordpress.
for example:
<input type="text" name="user_name" value="Anonymous" required>

would you mind to change your
.html filename to .php filename
your codes runs in my local server because sometimes the difference lies in how your web server is configured.
when running in an web server it is best to use .php in your files

Related

PHP $_POST is not working

I've recently migrated servers and found that all $_POST requests on my website are no longer working. The requests always successfully redirect to the 'post' page, but no data is sent and print_r($_POST) always turns up an empty string. I believe this issue has something to do with PHP's settings because the problem occurs over multiple pages which were working fine on the previous server (GET requests seem to still be working fine).
I created a test.php file in my home directory with a very simple POST form to try to rule out any variables.
<?php print_r($_POST); ?>
<form action="test.php" method="POST">
Name: <input type="text" name="name"><br>
<input type="submit">
</form>
the page always prints only an empty the array Array ( )
Any help would be greatly appreciated, I'm sure its something simple but google doesn't seem to have the answer :(
I've realized the issue was in my .htaccess file. My POSTS were being stripped of their extensions and losing all the post data.
this topic php $_POST array empty upon form submission was the most help

form method only works with GET and not POST

So this never happen to me before until today.
I have a login form trying to send a simple post to the action form. var_dump returns array 0 when i use post method BUT when i use method = get and var_dump the get i get all the strings.
here is my html code
<form name="mlsnavform" id="mlsnavform" action="script/mlsnavform.php" method="POST">
<input id="uname" name="uname"type="text" placeholder="Username" autocomplete="off">
<input id="loginbtn" name="loginbtn" type="submit" value="SIGN IN">
</form>
here is mlsnavform.php
<?php
var_dump($_POST);
?>
And the result of this is array(0) { }
But when i change the method from post to get, and use var_dump($_get) instead of $_post i DO get the values but ofcourse login credentials showing up in your URL address is not something i would ever want to do.
Using get fills my array with what i need tho
array(2) { ["uname"]=> string(9) "damnitphp" ["loginbtn"]=> string(7) "SIGN IN" }
This never happen to me, i've been scrolling on stack for almost 2 days now for a "posible duplicate" before i decide to make a new post but there was only 1 i found which had an answer talking about "PHPStorm" but i do not use phpstorm. I use notepad++(yes i could use sublime or atom or brackets but im so used to notepad i dont feel the need of switching to another text editor).
Anyone know any fix for this? thanks in advance!
EDIT: So today i decide to open a project i made 4months ago that always worked, but the post from that page isnt working either. So what did i do? i decided to to remove that 1 thing i added a couple of weeks ago before all these issues started.
URL REWRITE from IIS so i can remove the .phpextension
So basically this was my issue all along but so far googling around there isnt a single topic on the net where other people are talking about this. If there maybe a similar issue to this. would much appreciated if someone can comment me the link or a solution.thanks in advance.
So my issue is "$_POST" doesnt work when i have my URL Rewrite code in the webconfig. if i remove the rewrite code then the .php extension shows in the browser(which i don't want) but my $_post methods work again.
You can check the enable_post_data_reading in php.ini
From php.ini:
; Whether PHP will read the POST data.
; This option is enabled by default.
; Most likely, you won't want to disable this option globally. It causes $_POST
; and $_FILES to always be empty; the only way you will be able to read the
; POST data will be through the php://input stream wrapper. This can be useful
; to proxy requests or to process the POST data in a memory efficient fashion.
; http://php.net/enable-post-data-reading
Check if this option is uncommented and comment this with ";"
;enable_post_data_reading = Off
You probably are taking the superglobal variable names POST and GET too literal. $_GET is the parsed query string, it's independent of the HTTP method, i.e. it will always be present, even with POST requests. $_POST is the parsed HTTP body when conforming to some constraints (the content-type header has to specify either application/x-www-form-urlencoded or multipart/form-data, and I think the method really has to be "POST" - Note that when NOT conforming to the constraints, even if you'd use the POST method, you won't get any data inside $_POST.
<form name="mlsnavform" id="mlsnavform" enctype="application/x-www-form-urlencoded" action="script/mlsnavform.php" method="POST">
<input id="uname" name="uname"type="text" placeholder="Username" autocomplete="off">
<input id="loginbtn" name="loginbtn" type="submit" value="SIGN IN">
</form>

Can't get POST value when submitting form

So here's my HTML form
<form method="POST" action="/process_forgot">
<input type="text" name="name" value="test">
<input type="submit">
</form>
And /process_forgot
if(isset($_POST['name'])){
echo "good";
}
else{
echo "string";
}
And all I get back is string. Which is weird because I'm posting the value and I set the name. I've done this tons of times, this is the only time i've ever had an issue. Any ideas?
Add the .php extension to the file location process_forgot...this should fix the issue because without that you have a redirect and all the POST data are lost; for this reason it always runs echo "string".
1) It may sound dumb, but try to see if you require a .php extension in the end of process_forgot in your action.
2) Try directly going to [YourWebsite]/[YourDirectory]/process_forgot.php and see if the file is accessible. You will, depending on your code, probably get a blank page, but that is expected.
3) Check if there is any other problem in your code that is preventing the script from running properly.

Posting from IE8 to PHP gives blank $_POST

I have a simple HTML form, sending a post request to a php script. In IE8, the form only works intermittently - most of the time the PHP script sees an empty $_POST variable.
Here's my code:
<html>
<head>
<title>Post test</title>
</head>
<body style="text-align: center;">
<?php
echo "<pre>".print_r($_POST, TRUE)."</pre>";
?>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="name">
<input type="hidden" name="hidden" value="moo" >
<input type="submit" value="Search" >
</form>
</body>
</html>
Sometimes the print_r gives the response you'd expect (i.e. it's populated with the data from the form), most of the time it's empty.
Not being able to use POST is a bit of a problem for web applications - anyone got any ideas what's going on, and how to fix it?
Thanks everyone for wading in on this one.
It turns out the problem lay in an Apache module I had enabled.
It's a module to allow apache to use Windows authentication to identify a user via their Windows User id - mod_auth_sspi
The effect is caused by a known bug, in the module, but with a simple extra directive this can be worked around, until a fix is added in the next update, as described here:
http://sourceforge.net/projects/mod-auth-sspi/forums/forum/550583/topic/3392037
That sounds very very bizarre. Does it happen in other versions of IE as well?
I can't tell you what the problem is, but here are my suggestions on how to diagnose it:
Print $_REQUEST rather than just $_POST, to see if the data is coming in via another method.
Use a tool like Fiddler or Wireshark to track exactly what is actually being sent by the browser.
Fiddler in particular has been very helpful for me a few times (mainly when debugging Ajax code), and will tell you exactly what was posted by the browser. If your web server is localhost, you can also use Fiddler to track what is received before PHP gets its hands on it. If not, you can use wireshark on the server if you have permissions for installing that sort of thing.
In addition to Fiddler, I would have suggested a browser-based tool like Firebug, but I don't know of one for IE that is good enough (The IE dev toolbar doesn't give you details of request and response data, as far as I know).
I'm suspicious that when the script is telling you that $_POST is empty, you did not actually POST the form. You can check by adding print($_SERVER['REQUEST_METHOD']); after your print_r($_POST);
If you are posting a file some of the time (i.e. with a file input) then make sure you set enctype="multipart/form-data" in your <form> element.
Have you checked the generated html? Is it possible that echo $_SERVER['PHP_SELF'] isn't producing the output you're after, which messes up the form html, which messes up the POST?

Form calls 2 php functions

I have an html form which uses a PHP file to submit data by email. I want to add some code (which I already have) to generate random numbers for spam protection. Can I call another PHP file within my form?
Here is the code that goes in the form:
<form name="mail" action="go.php" method="post" onsubmit="return CheckData()">
<input type="text" name="q">
<input type="submit" name="Submit" value="OK">
</form>
I am a real novice with PHP so any help would be appreciated.
LozFromOz
you can do it with image in your form that call to php file.
the famous is to use captcha,
read this link :
Stopping scripters from slamming your website hundreds of times a second
a good captcha to insert in php :
http://recaptcha.net/plugins/php/
There's no need to have the browser make two http requests for two different urls to the webserver. Your php script go.php can do what ever you want it to do, e.g. include two other scripts and/or calling two functions or ...
<?php // go.php
require_once 'spam_protection.php';
require_once 'form_helper.php';
require_once 'database_something.php';
....

Categories