I'm trying to POST form data from my html page to PHP, but am unable to see the data which I've posted. Kindly help. This is my code:
Code for html file:
<!doctype html>
<html>
<head>
<title>
Intro Page.
</title>
</head>
<body>
<form action="receive.php" method="POST">
Name:<input type="text" name="username">
Password:<input type="text" name="password">
<input type="submit" value="submit">
</body>
</html>
And the code for my receive.php file is:
<?php
$name=$pass="";
$name=$_POST["username"];
$pass=$_POST["password"];
if($_SERVER["REQUEST_METHOD"]=="POST")
{
$name= test_input($_POST["username"]);
$pass= test_input($_POST["password"]);
}
function test_input($data)
{
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
echo $name;
echo $pass;
?>
I'd start by changing your form submit button to have a name:
<input type="submit" name="submit" value="submit>
Allowing you to modify your PHP to look like:
function test_input($data) {
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
$name=$pass = '';
if(isset($_POST['submit'])) {
$name = test_input($_POST['name']);
$pass = test_input($_POST['pass']);
}
echo $name;
echo $pass;
You're probably receiving errors that you don't know about. While debugging/writing your code, it's best to have error reporting turned on by having the following at the top of your PHP scripts:
<?php
ini_set('display_errors', 1);
error_reporting(-1); // or E_ALL
1)check php installed properly
2)know that file place under apache root directory
TESTING :
open the receive.php directly and check first php working or not.
HTML
<input type="submit" name="form1_submit" value="submit">
as per @kunruh mentioned that
You are missing your closing </form>
PHP
<?php
$name=$pass="";
function test_input($data)
{
$data=trim($data);
$data=stripslashes($data);
$data=htmlspecialchars($data);
return $data;
}
//you have to check the value post like because some times more than one form in your html means it make conflict so you have to use name isset .
if(isset($_POST['form1_submit'])) {
{
$name= test_input($_POST["username"]);
$pass= test_input($_POST["password"]);
echo $name;
echo $pass;
}
?>
Make sure the receive.php file placed near the html file. Eg:
if the html file is place in the following path /var/www/html/YOUR_HTML_FILE.html
then the receive.php file should also be placed in the same path /var/www/html/receive.php. Else, mention the correct path in the form action <form action="/var/www/html/receive.php" method="POST">
. Also change the password field from text to password.
Text field - <input type="text" name="password">
<br>
Password field - <input type="password" name="password">
</div>
try something like this...
<html>
<body>
<form action="welcome_get.php" method="get">
Name: <input type="text" name="name"><br>
E-mail: <input type="text" name="email"><br>
<input type="submit">
</form>
</body>
</html>
and "welcome_get.php" looks like this:
<html>
<body>
Welcome <?php echo $_GET["name"]; ?><br>
Your email address is: <?php echo $_GET["email"]; ?>
</body>
</html>