0

Code does not work when the user submit after entering name and age. Page should submit to the same page and the HTML form should also display along with the result below. Please help me guys!!

<html>
<head>
 <title>My first PHP page</title>
</head>
<body>
  <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- $_SERVER['PHP_SELF'] array -->
   Name: <input type="text" name="name"/>
   Age: <input type="text" name="age"/>
   <input type="submit"/>
  </form>


</body>
</html>
<?php
/* because both the HTML form data and the PHP processing code are in the same script, you will need a conditional check to 
   see if the form has been submitted */

    if ( isset($_POST['submit']) ) { //was the form submitted?
        print "Raveen";
        //echo "Welcome ". $_POST["name"] . "<br>";
        //echo "You are $_POST["age"] years old<br>";
        //echo "The path to this file is: $_SERVER[PHP_SELF]";
    }
?>
Ishani
  • 53
  • 1
  • 2
  • 7

1 Answers1

0

you need give name to get post name

<input name="yourformname" ...

after that you can check the post name

like:  if ( isset($_POST['yourformname']) ) {

it must be like this

 <input name="submit" type="submit" />

Also there is another way to understand how to get the post. forexmaple create a hidden input in your form like:

    <html>
    <head>
        <title>My first PHP page</title>
    </head>
    <body>
            <form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post"> <!-- $_SERVER['PHP_SELF'] array -->
                Name: <input type="text" name="name"/>
                Age: <input type="text" name="age"/>
<input type="hidden" name="gotit"/>
                <input type="submit"/>
            </form>


    </body>
    </html>

After that you can check

<?php
/* because both the HTML form data and the PHP processing code are in the same script, you will need a conditional check to 
   see if the form has been submitted */

    if ( isset($_POST['gotit']) ) { //was the form submitted?
        print "Raveen";
        //echo "Welcome ". $_POST["name"] . "<br>";
        //echo "You are $_POST["age"] years old<br>";
        //echo "The path to this file is: $_SERVER[PHP_SELF]";
    }
?>
Ahmet ATAK
  • 342
  • 2
  • 13