w3resource

PHP Exercises : Create a HTML form and accept the user name and display the name

PHP : Exercise-4 with Solution

Create a simple HTML form and accept the user name and display the name through PHP echo statement.

HTML form: A webform or HTML form on a web page allows a user to enter data that is sent to a server for processing. Forms can resemble paper or database forms because web users fill out the forms using checkboxes, radio buttons, or text fields. For example, forms can be used to enter railway or credit card data to purchase a product, or can be used to retrieve search results from a search engine.

Sample Solution:

PHP Code:

<!DOCTYPE html>
<html>
<head>
   <title></title>
   <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
   <!-- HTML form with POST method -->
   <form method='POST'>
      <h2>Please input your name:</h2>
      <!-- Text input field for the name -->
      <input type="text" name="name">
      <!-- Submit button to submit the form -->
      <input type="submit" value="Submit Name">
   </form>
   <?php
   // Check if the form is submitted and 'name' is set in $_POST
   if ($_SERVER["REQUEST_METHOD"] == "POST" && isset($_POST['name'])) {
      // Retrieve name from the form and store it in a local variable
      $name = $_POST['name'];
      // Display a greeting message with the entered name
      echo "<h3> Hello $name </h3>";
   }
   ?>
</body>
</html>

Sample Output:

sample html form

View the output in the browser

Explanation:

Here is a breakdown of the above PHP code:

  • HTML Structure:
    • The code starts with the usual HTML structure, including a head section with a title and character set information.
  • Form Section:
    • Inside the body, there is an HTML form with the 'method' attribute set to 'POST'. This form has an input field where users can enter their name and a submit button to submit the form.
  • PHP Section:
    • The PHP code begins with <?php and ends with ?>. It is embedded within the HTML and will be executed on the server side.
    • The PHP code checks if the form has been submitted ($_SERVER["REQUEST_METHOD"] == "POST") and if the 'name' field is set in the $_POST array (isset($_POST['name'])).
    • If the conditions are met, it retrieves the entered name from the '$_POST' array, stores it in a local variable '$name', and then displays a greeting message using 'echo'.
    • The greeting message is wrapped in HTML tags (<h3>) and includes the entered name. This message will only be displayed if the form has been submitted and the 'name' field is not empty.
  • End of HTML:
    • The HTML body and document end tags (</body> and </html>) close the document.

Flowchart:

Flowchart: Create a HTML form and accept the user name and display the name

Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: $var = 'PHP Tutorial'. Put this variable into the title section, h3 tag and as an anchor text within an HTML document.
Next: Write a PHP script to get the client IP address.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Become a Patron!

Follow us on Facebook and Twitter for latest update.

It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.

https://www.w3resource.com/php-exercises/php-basic-exercise-4.php