Related post

PHP Forms :How to access data submitted by user in PHP - Zhullyblog



Hello, welcome to zhullyblog. If this is your first time here , please Subscribe to Zhullyblog by Email to get latest tutorial.


How to Access data submitted by the user  in PHP



In this tutorial, you will learn to access and collect data submitted through a  a form in HTML.

When you create a website, you need to get some details about the user on your website. This is achieved by creating a form. When a user submit their data, as an admin, you need to access the data and process it and store it for several purpose. The data’s could be Email , phone number, comment etc.


In the case of comments, you need to see whatever comment your user post.
Before you access data, you need to create a form that the user will fill and input the data you ask them to. So, you need to create the data in HTML.



Create a contact form in HTML




The code below allows user to enter their name, enter email and drop a comment. Open any editor of your choice and save it as 'comment.php’.

This tutorial will teach you basically how to collect data submitted by user using the PHP superglobal variables $_GET, $_POST and $_REQUEST. Click here to learn more about $GET and $POST method

So here, we would be working in receiving user name, Email and comment and then display the details back to the user.




<!DOCTYPE HTML>
<html>
  <head>
    <title>Make a comment</title>
  </head>
  <body>
    <p>Please enter your comment</p>
    <form action="comment.php" method="post">
      <p>
        <label for="inputName">Name:</label>
        <input type ="text" name="name" id="inputName"></p>
        <p>
        <label for="inputEmail">Email:</label><br/>
        <input type="text" name="email" id="inputEmail">
      </p>
      <label for="inputComment">Comment</label>
      <textarea name="message" id="inputComment" rows="7" cols="40"></textarea>
      <input type="submit" value="Submit">
    </form>
  </body>




Here is a screenshot



Output


In the form tag, we have two attributes which are action and method attributes. And right now, you might the wondering what each attribute are used for right. Well let's break it down. Click here to learn about attributes.
• The action attribute receives data submitted by user when they press the submit button.


• The method attribute tells the browser to send the form data through POST method.


• The comment.php is being processed on the server once the user click on the submit button.

Click here to learn more about HTML form.


Once the user has entered the necessary information, you as an admin need to access those information. But to do this, you use the superglobal variables.


So once the user has filled the form, you will receive it then display it back to the user. But you also need to check whether the user fills the entire form or the important part of the form. Checking whether user fills the form is called validation. Click here to read about form validation


Here is the PHP code for comment.php.

Here , we are assuming that the user has clicked the submit button and we have received the details. So , the nest step is to display the details back to the user and thank the user for making a comment.

PHP script




<!DOCTYPE HTML>
<html>
  <head>
    <title>Subscribe</title>
  </head>
  <h2>Thanks for making a comment</h2>
  <p>Here is the your mail and the comments you made</p>
  <ul>
    <li>Name:<?php echo $_POST["name"]?></li>
    <li>Email:<?php echo $_POST["email"]?></li>
    <li>Comment:<?php echo $_POST["comment"]</li>
  </ul>
</html>




This main idea to the above code is that the data’s are sent through the post method, then you can retrieve it and then display it using the echo statement. Click here to learn about Echo statement






Please share
OTHER TUTORIALS



OTHER TUTORIALS

 Follow us on

Get the latest tutorials and updates




Delivered by FeedBurner

Comments

  1. Thank you because you have been willing to share information with us. we will always appreciate all you have done here because I know you are very concerned with our. PHP essentials

    ReplyDelete

Post a Comment