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

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.

Related

$_POST request not being handled by the server

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

(PHP) I can't get a form action PHP code to work

I am not sure what i am doing wrong, but i cannot get a PHP form action page to work, when i attempt to run it, it takes me to a blank page that does nothing. I am trying to create a simple code that redirects the user to a page depending on the input on the form. Here is what my form code looks like:
<form action="index.php" method="post">
<font face="Stymie">ENTER SECRET CODE: <input type="text" name="secretcode"></font>
</form>
and here is what the index.php currently looks like:
<html>
<body>
<?php
if ($_POST['secretcode'] === "banana") {
header("Location: banana.php");
}
?>
</body>
</html>
This is one out of many codes i attempted to use, yet no matter what i put, it always takes me to a blank page. I also tried using GET instead of POST, but that just leaves me on the page without the code running.
Can someone tell me what i am suppose to do to get the code working? Help would be much appreciated.
UPDATE: I found a Javascript equivalent to this script which is a lot more efficient and simpler, thus i no longer need an answer.
<form onSubmit="return checkAnswer();">
<script>
function checkAnswer(){
var response = document.getElementById('secretcode').value;
if (response == "banana")
location = 'banana.html';
else
location = 'wrong.html';
return false;
}
</script>
Add a submit button:
<form action="index.php" method="post">
<font face="Stymie">ENTER SECRET CODE: <input type="text" name="secretcode"></font>
<button type="submit" name="submit" value="Enter"/>
</form>
And for the php code:
<?php
if (isset($_POST['submit'])) {
if($_POST['secretcode'] === "banana"){
header("Location: banana.php");
}
}
?>
I added the isset() to prevent possible Undefined index: errors when you expand the php codes
UPDATE
I just tried OP's codes, it is submitting even without a submit button (didn't know that, or maybe I just forgot).
It seems working for me, I am being redirected to banana.php when I typed banana in the input, the only problem I saw is the Undefined index error (which the isset() will solve it)

How to run a php code, only if the "submit" button redirect you.

This is my admin panel code:
<form action="connectdb.php" method="post">
<input type="text" name="name">
<input type="submit">
</form>
So, It so, the code in connectdb.php will only run, if the "submit" button redirects a user to it. It will not run, if a user directly open /connectdb.php page.
Do I need to start some session, something like that?
Note: I am a newbie, so please explain in detail.
Since your form is using method="post"you can place the following code at the very beginning of your connectdb.php file:
<?php
if (empty($_POST)){
exit;
}
//The rest of your code goes here
This checks to see if the $_POST variable either does not exist or does exist but is empty. If this returns true that means your form was not submitted and a user went to the page directly. The script will then exit and a blank screen will be displayed.
Instead of displaying a blank screen, you may instead want to redirect to a different page such as this:
<?php
if (empty($_POST)){
header("Location: index.html");
exit;
}
//The rest of your code goes here
Whenever you do a redirect like this, it is important to place an exit; statement directly after it, otherwise your script could still process some of the other statements and send data to the browser that shouldn't be sent. This of course could be a security risk in some cases. An exit statement prevents this kind of security risk.
Not sure if you really need it, but you can add a name attribute like the following:
<input name="submit_button" type="submit">
So when you click this button a $_POST['submit_button'] variable will be created on the PHP side, and then you can use it to check if the button was clicked:
if(isset($_POST['submit_button'])){
// your code
}
<input type="submit" name="submit_btn">
Now in your connectdb.php check,
<?php
if(isset($_POST['submit_btn']))
{
//do your code
}
else
{
//redirect to your home page
}
?>

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.

How to redirect page using $_SERVER['REQUEST_URI']

I'm building a page that has a form that's submitting to my database. The form needs to post to the current page to process, but since I'm using this same form on many pages, I would like a way to tell the form to use the user's current page to submit to. I know the code below can get this information, it's what I'm using to send url data to my database for another use.
$_SERVER['REQUEST_URI']
I'm just not sure how to use that within the code that loads the page. I suppose I could write something that will pull out the submitted url info that was just submitted to my database and then plug it in, but is there a way to just use the above code to do it? That seems easier, but when I tried plugging it in, it fails and just gives me a "404 not found" error, so I know I'm not doing it correctly.
It just needs to go in the simple form post action code below:
<form action="comment_box.php" method="post">
I've looked up similar questions, but I couldn't find anything that worked with what I needed, I just got more 404 errors. Any advice would be great, thanks!
If your posting to the same page the user is on you can do
<form action="" method="post">
Leaving the form action blank it will post to what ever the page the form is on.
Not sure what language your using but I assume php you then use
<?php
if($_SERVER['REQUEST_METHOD'] == "POST") {
// form submit button was pressed your php code here
} else {
// no form submit button was press do nothing maybe show form html code here
}
?>
or what your trying to achieve
$url = $_SERVER['REQUEST_URI'];
Then in PHP
<form action="<?php echo $url; ?>/comment_box.php" method="post">
for example you have a following code
<input type="submit" name="click_me" value="Login">
then in the php, you have to check like
<?php
if(isset($_POST,$_POST['click_me']) && $_POST['click_me']="Login"){
//do your stuff
}
?>

Categories