$_POST request not being handled by the server - php

I have a form with one text input field:
index.html
<form method="post" action="comment.php">
<a href="javascript:void(0)">
<div class="avatar" style="background:url('img/user4561.jpg') center/cover"></div>
</a>
<input name="comm" type="text"/>
<input type="submit" value="submit"/>
</form>
And here is comment.php in the same directory
<?php
echo $_POST["comm"];
?>
Now the most incredible event. The form data does not get submitted! I get an error:
Notice: Undefined index: comm in 192.168.0.1/comment.php on line 2
I changed comment.php like this:
<?php
if(isset($_POST["comm"])) {
echo "It is set";
} else {
echo "It is not set";
}
?>
and I get:
It is not set
So, I copied the HTML code of the form into another blank webpage. I also changed the action attribute of the form to "192.168.0.1/comment.php". Now I get
It is set
I also tried using GET instead of POST, but I am again in the same conflict.
EDIT: I removed all other files and code, except for the form and its script. The problem persists. You can now read and modify the source code. Go to files.000webhost.com. Use chatstack as the website name and 6RxOkuJ1CIkbNqxKUMGr as the password.
EDIT: I modified the code for the form above to exactly match that in the above given link. I noticed that removing the link from inside the form solves the problem. I have already done this, but this is so weird. According to this post it is totally fine to have other elements inside a <form> element. There are also other parts of my website where I have <div> elements inside a form element and the form is working fine.
What is this? Is it the server or something else?
I am pretty sure the information provided here is not enough. Feel free to ask for any additional information. There is just too much information to write in a post, and I don't know which part of it is relevant.

Open your dev-console in browser.
Go to the network tab. Now you fill in your form and submit. After your comment.php is loaded, you check for the very first row in the network tab (should be the comment.php html document).
When you click on that request it should display you, whether the request was GET or POST and also what data were sent to this document.
You can also try at comment.php var_dump($_REQUEST); to see what data were sent and how it can be accessed.
By this you can see if server all over receives any data. If yes, then you go for an other server like apache2 for testing purpose to look if bug is fixed.
That's how I would to that, but I suspect that by editing your code for the public, you accidentally withheld some information that would solve this simple problem.
It's very unlikely that web-servers like WAMP had passed the testing environment before publishing a new version allthough no data could be procesed by server

Related

Wordpress form submit: do $_REQUEST and form POST method work with WP?

I have a hand-built (HTML/PHP) site that I would like to replicate on Wordpress. The site includes a form which sends the user input to a 3rd party server which returns a price quote which then gets displayed.
The structure is as follows:
<?php
if($_REQUEST['page']==''){
<form id=frm action='' method=post>
<input type=hidden name=page value=calcs>
...
<input type=submit id="submitButton" value='Get your instant quote'>
</form>
}
else
{
PACKAGE THE USER INPUT VIA XML AND SEND WITH A CURL COMMAND TO 3RD PARTY SERVER, RECEIVE REPLY IN XML.
FROM REPLY EVALUATE MINIMUM PRICE QUOTE, ECHO TO PAGE, EMAIL WEBMASTER.
}
?>
This works fine in my original HTML/PHP website, but I'm a noobie when it comes to Wordpress and I'm afraid I've bitten off more than I can chew :(((
In Wordpress I've made a page template page-form.php which resides in my theme folder. This file is very similar to the structure described above:
if($_REQUEST['page']==''){
include 'template-parts/form.inc.php';
}
else
{
PACKAGE THE USER INPUT VIA XML AND SEND WITH A CURL COMMAND TO 3RD PARTY SERVER, RECEIVE REPLY IN XML.
FROM REPLY EVALUATE MINIMUM PRICE QUOTE, ECHO TO PAGE, EMAIL WEBMASTER.
}
This Wordpress form displays correctly but when I fill it in and submit I land on a "Hello World" page with no price-quote and I recieve no email. form.inc.php is for all intents & purposes the same as the original form from the 1st version of the site.
Have I made some fundamental error in thinking I could transplant the structure straight to Wordpress? I have tried googling for similar issues but can't find anything, feels like I'm on the wrong path .... if anyone can give me a helping hand I'd be very grateful!

Why is my form using GET instead of POST?

When I try to submit a form from mysite/form to mysite/import using this form:
<form action="../import" method="POST">
<input type="text" name="hidden" value="hi">
<input type="submit">
</form>
The import site doesn't seem to recieve the post. I get redirected to the import site, but the data doesn't seem to be there. When checking I discovered this:
var_dump($_SERVER['REQUEST_METHOD']) gives string(3) "GET"
var_dump($_POST) gives array(0) {}
var_dump($_REQUEST) gives array(0) {}
When using the same form from the import site, everything works, and when I use the form page to some random other page, it also works.
I've already tried to add method='POST' to everything, or adding formmethod="post" formaction="../import" to the submit button, but nothing works.
My guess is that you have an intermediate redirect. There's nothing wrong with your code at first glance. Turn on developer tools in your browser (make sure the log is preserved so that it doesn't clear when the browser navigates to a new page), and watch the network activity. (You can also use a tool like Fiddler to do this). I'll bet you see a POST followed by a GET redirect to the final page.
The fact that the exact same code works on another site might indicate an .htaccess file or something in play.
found the problem.
Apperently there is something wrong with using mysite/import, and when I changed the action to action='../import/' (instead of action='../import') it worked.

PhpStorm - strange 404 error when using html forms

Running PhpStorm 2016.3.2, Ubuntu 16.04 with PHP7 and LAMP stack installed and tested.
I seem to have an error in PhpStorm (or somewhere else down the line), whereby clicking 'Submit' in an html form will result in a '404 not found' error, even if the form action path is valid.
I built a very simple application to demonstrate this, however it should be noted that this error has occurred every time I use an html form in PhpStorm so far, even example applications that I know have no errors in code.
The code for my form is as follows
var $form = <<< FORM
<form action='?' method="post">
<p>Enter your number </p>
<input type="text" name="formInput"
maxlength="10">
<input type="submit" name="formSubmit"
value="Submit">
</form>
FORM;
Once 'Submit' is clicked, the 404 error happens. I have tried multiple permeations of form action such as "" and even a variable set to APP_ROOT_PATH, and then just manually typing out the correct path (even a different php file) also with single and double quotes.
The reason I believe these paths to be valid is that simply by clicking in the address window from the 404 error page and hitting enter (thereby going to the path it was trying to get to) the form loads again fine.
I can't get my head around why this might be, if it was a permissions or path error surely I would not be able to access the path from the browser either?
EDIT: Looking at the source code reveals that the action is what I intended it to be. Even copying the url from there and pasting it into the browser will open the correct form. It is just when clicking 'submit' that the 404 error happens. The correct url is also in the address bar on the 404 page.
EDIT 2 : For note, the '?' action in the above code is irrelevant to the issue as the error occurs no matter what correct link I put there, be it "" or the full path.

Reading an hidden input field from form of a different PHP site

I have the following hidden input in a form:
<input type="hidden" name="token" value="<?php echo $token; ?>">
I am posting this input value to another PHP page for processing through a form, however, when I try to read the value of the input field using $_POST["token"], I find it empty. I looked all over the Internet for a solution, but the only one I found is to place everything into one page (the form together with the processing code); but I want the processing code to be in a separate page.
This is the markup:
<form id="registerform" name="registerform" method="post">
First name:<br>
<input type="text" name="firstname"><br>
Last name:<br>
<input type="text" name="lastname">
<input type="hidden" name="token" value="<?php echo $token; ?>">
</form>
The problem here is that I can read the values of the first name and last name. but the value of the hidden input is not accessible.
Your approach is correct. If it won't work like this, you probably made some typo or mistake and need to debug your code. Here is one way to do that.
Check if the value $token is correctly rendered into the <input>. Does it have a value in the PHP script or is already empty there? If it is not empty in the PHP script (you can echo it out, to see its value), you need to check the rendered HTML. Does it have any mistakes? It's best to just open the view-source tab to see what PHP actually renders. Somewhere in the rendered HTML, there needs to be a hidden input field with the value set to the same as in the PHP script. Check also the syntax.
If 1 did not yet resolve your problem, you can try to send the form and observe the request in the DevTools of your browser (it's probably best to use Firefox or Chrome for that). To see the request, you should open the DevTools (push F12) and switch to the Network tab. Make sure, you hit Preserve log to keep the logs on the page switch. Click on the row of the page where your data should have been sent to. You can see the parameters on the bottom of the details. Your hidden parameter needs to be there, if not, you are not correctly sending it to the PHP script. → Go back to step 1.
If the error still resists, enter the following line on top of the PHP page where your form is sent to (the page you have in the action attribute of the form): <?php print_r($_POST); ?>. This will print out all data which is received by the PHP script via POST (change it to $_GET, if you are expecting GET data). Submit your form. If the token key is present on the next page (e.g. [token] => "123" is part of the output), you can definitely access it via $_POST['token'].

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?

Categories