LERAN PHP COMPLETE

Learn one of the most powerful programming languages in the world and become a rockstar developer.

PHP Form Introduction


What Are Forms?

In HTML, we can create forms with inputs like text fieldstext areascheckboxesradio buttons, etc. to get some data from the user. Then, we can submit the user input to the server and process them.

In web development, handling a HTML form is perhaps the most important process. Two steps are involved:

  • Creating the HTML form
  • Creating the corresponding PHP script that will receive and process the form data

Where Are Forms Used?

Almost every website has forms. Including,

  • Comment forms
  • Subscribe forms
  • Contact us forms
  • Search Forms

Forms are really handy when creating dynamic websites.

Example

<html>

    <head>
        <title>PHP Information Registration</title>
    </head>

    <body>
        <?php

            $name = $email = $gender = $comment = $website = "";
            if ($_SERVER["REQUEST_METHOD"] == "POST") {
                $name = test_input($_POST["name"]);
                $email = test_input($_POST["email"]);
                $website = test_input($_POST["website"]);
                $address = test_input($_POST["address"]);
                $gender = test_input($_POST["gender"]);
            }

            function test_input($data) {
                $data = trim($data);
                $data = stripslashes($data);
                $data = htmlspecialchars($data);
                return $data;
            }
        ?>

    <h2>Adzetech Information registration</h2>

    <form method = "post" action = "">
        <table>
                <tr>
                    <td>Name:</td>
                    <td><input type = "text" name = "name"></td>
                </tr>

                <tr>
                    <td>E-mail:</td>
                    <td><input type = "text" name = "email"></td>
                </tr>

                <tr>
                    <td>Address:</td>
                    <td><input type = "text" name = "address"></td>
                </tr>

                <tr>
                    <td>Website:</td>
                    <td><input type="url" name = "website" ></td>
                </tr>

                <tr>
                    <td>Gender:</td>
                    <td>
                          <input type = "radio" name = "gender" value = "female">Female
                          <input type = "radio" name = "gender" value = "male">Male
                    </td>
                </tr>

                <tr>
                    <td>
                        <input type = "submit" name = "submit" value = "Submit">
                     </td>
                </tr>
        </table>
   </form>

<?php
          echo "<h2>Your Given details are as :</h2>";
          echo $name;
          echo "<br>";

          echo $email;
          echo "<br>";

          echo $website;
          echo "<br>";

          echo $address;
          echo "<br>";

          echo $gender;
?>

</body>
</html>

Output

php form introduction demo

x
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Privacy Policy That's Fine