Simple PHP form not working

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <form action="index1.php" method="POST">
   <input type="text" name="inputname">
   <input type="submit">
  </form>
</body>
</html>
<?php
    echo $_POST["inputname"]
?>

I don’t understand the reason behind this not working.

you always need to put a semicolon ; at the end of an echo statement. i.e echo $_POST["inputname"];
and always remember to check for $_POST before the echo statement i.e.

<?php
if(isset($_POST) && isset($_POST["inputname"])){
     echo $_POST["inputname"];
} 
?>
1 Like

Now I am getting it as

It is working fine for me.
first I created a index.php which contains the following code:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>Document</title>
</head>
<body>
  <form action="index1.php" method="POST">
   <input type="text" name="inputname">
   <input type="submit">
  </form>
</body>
</html>

second I created an another file index1.php with following code:

<?php
if(isset($_POST) && isset($_POST["inputname"])){
     echo $_POST["inputname"];
} 
?>

the result of the above code is :
before
when I click on submit button the result is :

after

Hello!

Why did you open the file directly on the browser? If you open a file using the browser, then that file is not interpreted by the server, hence it will not show what you expect.

If you want to access it directly, then open http://localhost/index1.php, which should be an empty page.

On the other hand, if that page is shown when submitting the form, then that may be a configuration problem with your server (it may not even be running).

2 Likes

Thanks man :slight_smile:
I found the issue.

1 Like

Thanks for helping out.