0

I created two inputs for name and age and when the user clicks on submit, the screen has to display the values he entered in the two inputs but it does nothing.. here are my codes

<html>
<head></head>
<body>
<?php 
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $age=$_POST['age'];
    echo $name;
    echo $age;

}
?>
<form action="" method="post">
<input type="text" name="name" placeholder="name"></br>
<input type="number" name="age" placeholder="age"></br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Hamza Khan
  • 23
  • 4

4 Answers4

0

You have to set the values of the input fields instead of just echoing them:

<html>
<head></head>
<body>
<?php 
$name = "";
$age = "";
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $age=$_POST['age'];
}
?>
<form action="" method="post">
<input type="text" name="name" placeholder="name" value="<?php echo $name;?>"></br>
<input type="number" name="age" placeholder="age" value="<?php echo $age;?>"></br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Iarwa1n
  • 460
  • 3
  • 12
0

Use $_SERVER['PHP_SELF'] in action to send data to same page.

<html>
<head></head>
<body>
<?php 
if(isset($_POST['submit'])){
    $name=$_POST['name'];
    $age=$_POST['age'];
    echo $name;
    echo $age;

}
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<input type="text" name="name" placeholder="name"></br>
<input type="number" name="age" placeholder="age"></br>
<input type="submit" name="submit" value="submit">
</form>
</body>
</html>
Neinrappeur Zaki
  • 397
  • 2
  • 12
0

I think you just have to check your server because I just cut and past your code and it works on my server with PHP7. Here is your code on one of my web site: http://www.ansb-brasil.org/go.php

Peter
  • 1,247
  • 19
  • 33
-1

you have to add an action for you form (to execute and send data to itself)

   <html>
    <head></head>
    <body>
    <?php 
    if(isset($_POST['submit'])){
        $name=$_POST['name'];
        $age=$_POST['age'];
    }
    ?>
    <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
    <input type="text" name="name" placeholder="name" value="<?php echo $age; ?>"></br>
    <input type="number" name="age" placeholder="age" value="<?php echo $age; ?>"></br>
    <input type="submit" name="submit" value="submit">
    </form>
    </body>
    </html>
  • 3
    you may find this useful: http://stackoverflow.com/questions/1131781/is-it-a-good-practice-to-use-an-empty-url-for-a-html-forms-action-attribute-a – Federkun Dec 11 '16 at 08:56