Form won't post on localhost - php

I have no idea what is going on here, but my form won't post. I've stripped it back to absolute basics, but still no joy. Get works fine and I'm working on MAMP on localhost.
Here it is: simplest form in the world that won't work.
<?php
print_r($_POST);
?>
<!DOCTYPE html>
<html lang="en" class="">
<head>
<title>Site Title</title>
</head>
<body class="wrapper">
<p>This is the form</p>
<form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="post">
<input type="text" name="firstName" />
<input type="submit" name="fSubmit" value="submit" />
</form>
</body>
</html>
I've probably missed something so stupid, hence getting some more eyes on it!
Cheers

Alright folks. Thanks for all your ideas - I had already tried EVERY POSSIBLE combination of actions, names, etc... I had the corret hml headers posted to the page. I just restarted my mac and re-ran the servers and everything seems to be ok. I have no idea what went wrong, but it seems to be fixed now.
Apologies for the fairly useless question.

maybe try
<?php
print_r($_POST);
?>
<!DOCTYPE html>
<html lang="en" class="">
<head>
<title>Site Title</title>
</head>
<body class="wrapper">
<p>This is the form</p>
<form action="" method="post">
<input type="text" name="firstName" />
<input type="submit" name="fSubmit" value="submit" />
</form>
</body>
</html>
or even just make sure php is installed/enabled

Related

having trouble with XAMPP; $_POST php not working

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.

$_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.

PHP print message

i want to print a message from php code, when the user clicks on button. I added code in php, and placed and if condition that executes when the user clicks on the button.
Here's the code of default.php:
<?php
if (isset($_POST['button1'])) {
echo '<b>Ok.</b>';
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script type="text/javascript">
</script>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP3</title>
</head>
<body>
<p> </p>
<p> </p>
<p> </p>
<form id="form1" name="form1" method="post" action="default.php">
<p>
<label for="textfield1">User Name</label>
<input type="text" name="username" id="username" />
</p>
<p>
<label for="textfield2">Password</label>
<input type="password" name="password" id="password" />
</p>
<p>
<input type="submit" name="button1" id="button1" value="Submit" />
<br />
<br />
<label id="label1">
</label></p>
<p> </p>
</form>
<p> </p>
</body>
</html>
"Ok" should be printed.
But i see no "Ok" message get printed.
What can i do?
Thanks
Could you try to add <?php phpinfo(); ?> in your body tag ? It should confirm if php is installed on your server.
Edit, just saw your last message, if your server is not running PHP, then you have to install it to achieve the result you are looking for.
if you're just testing your PHP file locally then try installing XAMMP to test your php file again, if it works then PHP wasn't installed before. XAMMP includes PHP, MySQL and Apache.
you should submit the form to itself or leave it blank. Check the form action and make sure you are posting to right php file.
You're echoing "Ok" before the DOCTYPE declaration -- it may be that it's there and you're not seeing it in the browser. Have you tried viewing the source and making sure your "Ok" isn't really there?
You might want to move your php code into the body part of your document.
If you say you can't see the PHP code when you click view source in your web browser you don't have PHP installed on your server. You need PHP to run this script. You would need to get in touch with your web host or enable it on your web server if its your own.

html form-php question

This is my html form and below this i included my php text.
But I am not getting correct output,i don't no where the problem is ?
I also included the output ,please suggest me what shuld i do?
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" />
</form>
</body>
</html>
This is my php text:
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
Output:
reading data from text field
Thanks for answering,
problem is that ,data send is not included after response of sever
please help me as fast as possible
I can only speak for my own experience, but this works on my server. I'd suggest, then, that one of the following is true:
Your server isn't set up to handle php (though this would surprise me), also, as #gAMBOOKa noted (in the comments), if your server's not set up to handle php the script wouldn't output anything other than the raw text-contents of the php script, literally "<?php echo $_POST["data"];?>".
You're trying to access the pages through the filesystem (file:///path/to/file.html), rather than through your server (http://localhost/file.html).
If '2' is correct, move your web-page and php script into a directory within your server's document root, on *nix this could be something like /var/www/directoryName/ and access the page via http://localhost/directoryName/dataEntryForm.html. If you're on Windows, with IIS it could be C:\inetPub\directoryName\ accessed, as above, with http://localhost/directoryName/dataEntryForm.html.
Incidentally, please forgive me for not linking to a demo of the page running, it's just that I'd prefer not to run the risk of exposing my server to, presumably, vulnerable scripts.
Your full code is like this and I tested it, working perfectly.
<html>
<head>
<title>
Entering data into text
</title>
</head>
<body>
<h1>
Entering data into text
</h1>
<form action="text.php" method="post">
What is ur name ?
<input type="text" name="data" />
<input type="submit" value="send" name="btn_save" />
</form>
</body>
</html>
text.php
<?php
if(isset($_POST['btn_save']))
{
$data=$_POST['data'];
}
?>
<html>
<head>
<title>
Reading data from textfields
</title>
</head>
<body>
<h1>
reading data from text field
</h1>
Thanks for answering,
<?php echo $_POST["data"];?>
</body>
</html>
working perfectly.

$_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