"Welcome to our PHP 101 blog, where we demystify the world of web development. "

Saturday, January 20, 2024

Retrieve data from Form fields

Create A FORM

Access the input data from form fields and display them in same page .

step 1: Create a simple form of 3 fields named as name, age and email , named it student.php

<html>

<head>

    <title>Student Database</title>

</head>

<body>

    <h2>Student Form</h2>

    <form action="" method="POST">

        <fieldset>

            <legend>Student information:</legend>

            Name:<br>

            <input type="text" name="name"> <br>

            Age:<br>

            <input type="text" name="age"> <br>

            Email:<br>

            <input type="email" name="email"><br>

            <br><br>

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

        </fieldset>

    </form>

</body>
</html>

step 2: add the php code in the same file named student.php , either above the html code or below the html code.

write the following code :

<?php

    if (isset($_POST['submit'])) {

        $name = $_POST['name'];

        $age = $_POST['age'];

        $email = $_POST['email'];

          }

?>

step 3: to display on the same screen add the following code , after the html code or php code
  

    <div class="container">
        <h2>Student Details</h2>
        <table class="table">
            <thead>
                <tr>
                    <th>Name</th>
                    <th>Age</th>
                   <th>Email</th>
                   
                </tr>
            </thead>
            <tbody>
<?php if($name!=0 && $age!=0 && $email!=0){?>
             
                       <tr><td><?php echo $name; ?></td>
                            <td><?php echo $age; ?></td>
                            <td><?php echo $email; ?></td>
                         
                        </tr><?php } else {
echo "Nothing to display";
}
?>
                       
            </tbody>
        </table>
    </div>



The execute this final code on server : 
http://localhost/ur_folder_name/student.php



No comments:

Post a Comment

Pages

SoraTemplates

Best Free and Premium Blogger Templates Provider.

Buy This Template