PHP Forms

Hi everyone,

We use $_GET and $_POST variables to retrieve information from forms, like user input. We are gonna talk about them later. But for now you should know this: Every form element in a HTML page is avaliable from our PHP scripts. It is very important because we often want to process these datas in our PHP codes.

Lets do an example that holds form values with POST method. Our main php file consists of two input fields and a submit button. When we click on submit button after filling up the input fields, it will send the input values in an invisible way to welcome.php file. And inside welcome.php file we are gonna use these datas in our php scripts. Here is how our main.php file looks like:

<html>
   <head></head>
   <body>
      <form action="welcome.php" method="post">
         Username: <input type="text" name="username"/>
         Age: <input type="text" name="age"/>
         <input type="submit"/>
      </form>
   </body>
</html>

In our browser we should see something like that:

Our welcome.php file should look like this:

Welcome <?php echo $_POST["username"]; ?>! </br>
You are <?php echo $_POST["age"]; ?> years old.

If we type Mehmet into username input field and 22 into age input field then click on the submit button we should see that output in welcome.php url location:

Welcome Mehmet!
You are 22 years old.

Now, you have learned about using HTML elements’ data  with PHP. Stay cool and stay connected to learn more! 😉

Leave a Reply

Your email address will not be published. Required fields are marked *