Can't get POST value when submitting form - php

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.

Related

PHP submit blank screen - script not running on POST?

I have a script that pulls an XML page and uses a form to update and save the values back. When I click the submit button it works, but then the page loads blank. I just want the page to refresh. There are about 100 different threads on this, and nothing I have tried has worked to resolve the issue. Out of curiosity, I just tried to run the window.location script and nothing else, and this piece actually doesn't work at all.
<?php
//if (isset($_POST['submit'])) {
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
//$ctstatus->nodeValue = $_POST['ctstatusform'];
//htmlentities($xml->save('test.xml'));
echo '<script type="text/javascript">window.location="http://google.ca";</script>';
}
?>
The inner contents of the form don't really matter at this point, I just want it to refresh the page after I hit the submit button.
I previously used isset but from reading it seems like that's obsolete, and my form action="" used to be blank. Either way my XML save works, but nothing to refresh the page. I also tried header and that didn't work either.
<form method="POST" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<input class="save" name="submit" type="submit" value="Save" />
</form>
Out of curiosity I tried an onClick function with a timer and this does work but it's not ideal at all, especially because the page could technically refresh before the POST is finished writing the file. I'd rather know why the echo doesn't execute.
PHP redirect would most likely be preferable to JavaScript redirect.
Typical structure when posting back to same page:
<?php // cannot be any output before this (space, linefeed, etc)
if(isset($_POST['submit']) {
// do stuff with the submission
header('Location: http://google.ca');
exit;
}
// does your script need to do some other data retrieval or calculation? do it here.
?>
<html>
... snip ...
<form method="post">
... snip ...
<input class="save" name="submit" type="submit" value="Save" />
</form>
Following this simple structure for procedural scripts--
Deal with user input / redirect
Do logic (collect, manipulate data)
Show output (using php only to insert variables and looping)
will help you avoid a lot of heartache and technical debt.
Okay, I have gotten this sorted out. It turns out that the problem was embarrassingly simple, but maybe will assist someone in the future. Along with reordering my code, as Tim suggested. I specified HTML as the DOCTYPE, and that worked to resolve the issue. I no longer need to worry about refreshing the page after submit, because it refreshes as it should automatically. Thank you to everyone who commented.

PHP Form action(POST) doesn't send to other page

I have an issue with my form.
Here's my form structure.
<form method="POST" action="../login.php">
<input type="submit" name="login" value="test">
</form>
It's really only that.
But on my login page, it cannot get the POST parameter, and error logs says nothing.
<?php
if(isset($_POST['login']))
{
echo "true";
}
else {
echo "false";
}
^^ That's my php code, right there. I've also tried with
if($_SERVER['REQUEST_METHOD'] == "POST"){}
Instead of isset.
Can anyone see what i do wrong here??
Kind regards
open login.php directly from URL and at the top of the code in login.php add the code given below.
<?php
echo "hello";die;
?>
if you see the hello on the page that means your login.php working fine. but if you not that means your file is renamed incorrectly sometimes it happens by our editor. the file becomes like login.php.txt
If the filename is correct and you are not getting hello then your server is not working.
and it seems your code is also fine.
You can also use POSTMAN to post data on your page without form and there is one more alternative ajax.
../login.php is the wrong path.
if everything works fine then the error is 100% in the path.

Form handling in php

I'm trying to set the value in my input text using the get method. However, when I try to render the page, it keep showing my code in the text field instead. Can someone explain what I did wrong?
<form name="quotepage" action="search.php" method="get">
<b>Stock Symbol:</b>
<input type="text" size="8" name="sb" value="<?php echo $_GET["sb"]; ?>" />
<input type="submit" value="Quote" onClick="quotepage.action='search.php';"/>
</form>
When I try to render the page, it will show my code in the value tag in my text field.
If you are seeing an error/warning/notice in your text box, try changing:
<?php echo $_GET["sb"]; ?>
into:
<?php echo isset($_GET["sb"])?$_GET["sb"]:""; ?>
to avoid showing the content if there was no content yet.
And better yet, change it to:
<?php echo isset($_GET["sb"])?htmlspecialchars($_GET["sb"]):""; ?>
to also escape nasty characters such as " that otherwise will break your HTML.
If you are actually seeing <?php echo $_GET["sb"]; ?> inside your text box, then you are having problems running PHP. Check that your script file name ends with .php and check PHP is working on your system.
Do you have a LAMP stack or similar set up?
You need Apache running with PHP installed at the least for this. Also what is this 'onClick="quotepage.action='search.php';"' attribute for?

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

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

PHP $_POST not working? [duplicate]

This question already has answers here:
PHP POST not working
(10 answers)
Closed 8 years ago.
I have the simplest form possible and all I want to do is echo whatever is written in text box.
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_POST['submit'])){
$test = $_POST['firstname'];
echo $test;
}
The problem is it's not working on my server (it works on another server). Does anyone has an idea what could be wrong? There are other forms on the server and are working fine.
I had something similar this evening which was driving me batty. Submitting a form was giving me the values in $_REQUEST but not in $_POST.
Eventually I noticed that there were actually two requests going through on the network tab in firebug; first a POST with a 301 response, then a GET with a 200 response.
Hunting about the interwebs it sounded like most people thought this was to do with mod_rewrite causing the POST request to redirect and thus change to a GET.
In my case it wasn't mod_rewrite to blame, it was something far simpler... my URL for the POST also contained a GET query string which was starting without a trailing slash on the URL. It was this causing apache to redirect.
Spot the difference...
Bad: http://blah.de.blah/my/path?key=value&otherkey=othervalue
Good: http://blah.de.blah/my/path/?key=value&otherkey=othervalue
The bottom one doesn't cause a redirect and gives me $_POST!
A few thing you could do:
Make sure that the "action" attribute on your form leads to the correct destination.
Try using $_REQUEST[] instead of $_POST, see if there is any change.
[Optional] Try including both a 'name' and an 'id' attribute e.g.
<input type="text" name="firstname" id="firstname">
If you are in a Linux environment, check that you have both Read/Write permissions to the file.
In addition, this link might also help.
EDIT:
You could also substitute
<code>if(isset($_POST['submit'])){</code>
with this:
<code>if($_SERVER['REQUEST_METHOD'] == "POST"){ </code>
This is always the best way of checking whether or not a form has been submitted
Dump the global variable to find out what you have in the page scope:
var_dump($GLOBALS);
This will tell you the "what" and "where" regarding the data on your page.
I also had this problem. The error was in the htaccess. If you have a rewrite rule that affects the action url, you will not able to read the POST variable.
To fix this adding, you have to add this rule to htaccess, at the beginning, to avoid to rewrite the url:
RewriteRule ^my_action.php - [PT]
Instead of using $_POST, use $_REQUEST:
HTML:
<form action="" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
PHP:
if(isset($_REQUEST['submit'])){
$test = $_REQUEST['firstname'];
echo $test;
}
Have you check your php.ini ?
I broken my post method once that I set post_max_size the same with upload_max_filesize.
I think that post_max_size must less than upload_max_filesize.
Tested with PHP 5.3.3 in RHEL 6.0
FYI:
$_POST in php 5.3.5 does not work
PHP POST not working
try doing var_dump($_GLOBALS).
A potential cause could be that there is a script running before yours which unsets the global variables. Such as:
unset($_REQUEST);
or even.
unset($GLOBALS);
This could be done via the auto_prepend_file option in the php.ini configuration.
try this
html code
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="firstname">
<input type="submit" name="submit" value="Submit">
</form>
php code:
if(isset($_POST['Submit'])){
$firstname=isset($_POST['firstname'])?$_post['firstname']:"";
echo $firstname;
}
There is nothing wrong with your code. The problem is not visible form here.
Check if after the submit, the script is called at all.
Have a look at what is submitted: var_dump($_REQUEST)
html file and php file both should reside in htdocs folder in c:\apache2 (if you use apache web server).
Open html file by typing http://"localhost/html_file_name.html"
Now enter Your data in fields.. Your code will run..
Try get instead for test reasons
<form action="#?name=test" method="GET">
<input type="text" name="firstname" />
<input type="submit" name="submit" value="Submit" />
</form>
and
if(isset($_GET)){
echo $_GET['name'] . '<br>';
echo $_GET['firstname'];
}

Categories