having trouble with XAMPP; $_POST php not working - php

I have a problem with $_POST function.
I have this simple code:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Title</title>
</head>
<body>
<?php
if (isset($_POST["password"])){
echo $_POST["password"];
};
?>
<form action="" method="POST">
Password: <br>
<input type="password" name="password"><br><br>
<input type="submit" name="submit" value="SUBMIT">
</form>
</body>
</html>
I run this code on PhpFiddle and it work as it should. However, when I running this on my laptop I get this error:
Notice: Undefined index: password
Thanks for your help. I am running XAMPP control pannel v3.22. IDE:PhpStorm. Php version: 7. OS: Windows 10.
I even asked this question here:
$_POST not working

You are missing to place a value inside the action attribute in your form. Replace this code:
<form action="" method="POST">
with this code:
<form action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>" method="POST">
The previous php code inside the action attribute informs the form that the input should be sent to the same page. The htmlspecialchars() function is used to prevent XSS attacks on your site.
EDITED:
You also forgot to place a closing parethesis. Replace the following code:
if (isset($_POST["password"]){
with
if (isset($_POST["password"])){
Let me know if that works for you.

Related

Why do I get "undefined array key" when trying to get values from HTML forms?

I have attached the code samples of my html and php files along with the error I'm getting whenever I try to
run the code to get values entered in my html form to my php document.
I'm using the XAMPP server.
I have done this exactly as mentioned in the site mentioned below:
https://www.w3schools.com/php/php_forms.asp
I would really appreciate it of someone could help me out with this.
HTML SAMPLE:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
</head>
<body>
<form action="welcome.php" method="POST">
Name : <input type="text" name="name"> <br><br>
E-mail: <input type="text" name="email"><br> <br>
<input type="submit">
</form>
</body>
</html>
PHP SAMPLE:
<!DOCTYPE html>
<html>
<body>
Hello <?php echo $_POST['name']; ?>
Your email address is <?php echo $_POST['email']; ?>
</body>
</html>
ERROR in my browser:
Warning: Undefined array key "name" in
C:\xampp\htdocs\BasicIntro\welcome.php on line 5
Your email address is
Warning: Undefined array key "email" in
C:\xampp\htdocs\BasicIntro\welcome.php on line 6
Both pages are working. no mistakes. I dont know why it does not work for you.
Try reinstalling XAMPP.

The requested URL [filename.php] was not found on this server

<?php
echo "hello";
?>
<!DOCTYPE html>
<html>
<head>
</head>
<body>
<form action="/one1.php" method="POST" enctype="multipart/form-data">
<input type="file" name="image" />
<input type="submit"/>
</form>
</body>
</html>
`
I have a Url on my server
This url is working. I have written echo "hello" in this.
but when i go through in this url i have written the html file upload code
It says The requested URL [one1.php] was not found on this server.
I checked my file_upload is on.
Make sure your 'action' attribute on form tag is set to /one1.php
I got the answer I contact the hosting team. They told to off the mod security in my panel and things the worked.

$_post remains empty after form submission

The post superglobal variable is empty after form submission; however, get works!
Here is the code:
index.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple Form</title>
</head>
<body>
<fieldset>
<form action="print.php" method="POST" >
first name:<input type="text" name="Fname"><br>
last name:<input type="text" name="Lname"><br>
<input type="submit" name="submit" value="submit">
</form>
</fieldset>
</body>
</html>
print.php
<?php
echo $_POST['Fname'];
echo $_POST['Lname'];
?>
same code works fine on my PC and print.php prints something but here on my Macbook $_post is always null after submitting the form.
I use IntelliJ IDEA as my IDE with Php plugin.
I use php 7
IntelliJ is not mapped to the MAMP server. That's why php is interpreted but POST does not work. In order to map MAMP to IntelliJ use this guide which works for IntelliJ ultimate edition and PhpStorm.

Guidance for Php for a beginner

I've just started to learn PHP. I found the $_POST variable is not working and posted the same at the below link
$_POST[] not working in php
and as per the advise i installed XAMPP. But still the proble of $_POST variable is not solved.
Now i've a doubt whether i need to configure any global variable to make $_POST work. I'm totally lost on this and dont know how to proceed.
Any help on this is verryy much appreciated.
Below is the html code - report.html
<html>
<title></title
<head></head>
<body>
<form action="report.php" method="POST" >
<label for="firstname">First name:</label>
<input type="text" id="firstname" name="firstname" value="TestOnly" /><br />
</form>
</body>
</html>
and below is the php code - report.php
<html>
<head>
</head>
<body>
<?php
print( $_POST['firstname']);
?>
</body>
</html>
Below is the view that i got from chrome network data
Thanks.
See, try this.
Source of index.htm File:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>POST</title>
</head>
<body>
<form method="post" action="post.php">
<input type="text" name="name" />
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Source of post.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
<title>Output</title>
</head>
<body>
<?php
if(isset($_POST["name"]))
echo "You have posted " . $_POST["name"];
else
echo "Nothing has been set!";
?>
</body>
</html>
Now try saving these two files under same directory. Enter something in the text box and click on submit. Let us know what you have got.
A few things to check:
Your script will have no data in the POST array unless a form has been submitted (Posted) to that script.
If you have a script and it's submitting properly, make sure the form has method="Post" in its opening tag. If your form submits and in the result script you see a bunch of variables in the address bar separated by ampersands (&) then it's using GET instead of POST. Make sure you have the above statement method="Post" in the form tag.
If you've submitted the script and you still can't see anything, try putting print_r($_POST) or var_dump($_POST) at the top of the target script to see a dump of the $_POST array. If this gives you nothing, try var_dump($_REQUEST) and see if your form data shows up there.
Have you ever tried if your php is correct?
<?php
phpinfo();
?>
or
<?php
print_r($_POST);
?>
what is your code? in default $_POST settings is active in every server unless in server ignore posted data for securitical reasons. i think you mistake is in your code ,don't change XAMP settings
not change the settings of php
either you use get method in html file and use post in action file in both you should use post.

$_POST in php 5.3.5 does not work

I'm working on a page in PHP 5.3.5, and it seems that $_POST doesn't contain the data submitted from my form.
This is the HTML file :
<html>
<body>
<form action="welcome.php" method="post">
Name: <input type="text" name="fname" />
Age: <input type="text" name="age" />
<input type="submit" />
</form>
</body>
</html>
And this is the PHP file:
<html>
<body>
Welcome <?php echo $_POST["fname"]; ?>!<br />
You are <?php echo $_POST["age"]; ?> years old.
</body>
</html>
The problem is that the form values are not passed, so I get output like this:
Welcome !
You are years old.
Instead of this:
Welcome John!
You are 28 years old.
What am I doing wrong?
Try <?php var_dump($_POST); ?> to see what's really in there. There's no way $_POST is broken - it's gonna be something else. Possibly a server setting could be disabling $_POST.
It's probably because you have magic_quotes_gpc turned on. You should turn it off:
http://www.php.net/manual/en/security.magicquotes.disabling.php
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
PHP 5.3 is moving away from using global, pre-set variables, like $_POST, to avoid vulnerabilities.
The issue, as I understand it, is that programmers who have never had to use $_POST or $_GET might treat it as any other variable and open themselves up to security threats.
I haven't discovered the "proper" way to retrieve $_POST data yet, but the method I've using seems fairly sanitary.
<?php parse_url(file_get_contents("php://input"), $_POST);
This transfers the parsed HTTP POST string to the $_POST variables
Is your HTML form method se to to POST?
<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>
You have not POSTed, or something is clobbering $_POST.
If you have no mention of $_POST in your code before trying to access them, then you haven't POSTed to your script.
Here it worked, but in PHP 5.3.1. Try this:
<!doctype html>
<html>
<head>
<title>form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<form method="post" action="process.php">
<fieldset><legend>your data</legend>
<input type="text" name="txtname" id="id_name" />
<input type="text" name="txtage" id="id_age" />
</fieldset>
<button type="submit">OK</button>
</form>
</body>
</html>
<?php
if(isset($_POST["txtname"]))
$txtname = $_POST["txtname"];
else
unset($txtname);
if(isset($_POST["txtage"]))
$txtage = $_POST["txtage"];
else
unset($txtname);
?>
<!doctype html>
<html>
<head>
<title>form</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>
<p>Welcome <?=$txtname; ?>!<br />
You are <?=$txtage; ?> years old.</p>
</body>
</html>
Try to use capitals in POST:
<form action="file.php" method="POST">
Instead of:
<form action="file.php" method="post">
I also had the same problem.I used windows notepad for editing .php file and accidentally saved the file in Unicode encoding and that was creating the problem. Then I saved it in the UTF-8 encoding, and it worked like a charm. Hope this might help.

Categories