Use of form data in PHP

To collect and use form data in PHP 

Checking the data for form submission in PHP

To check the form data in PHP

1. We use the isset()  function to check  that the variable is established or not .The isset() function return  false if variable testing  variable conatins a NULL value

2. $_POST is used to send the form data to the browser through the HTTP POST method.

form.php 

<?php
if (isset($_POST['submit'])){

    echo "<b>The form is submitted</b>";
}
?>

<html>
<head>
<meta charset = "UTF-8">//It is used for enable Unicode format text in the web page.
    <title> This is demo </title>
</head>
<body>
<form action= "form.php" method= "post">
    Name: <input type="text" name="name"> <br>

    E-mail: <input type="text" name="email"><br>

    Comment: <textarea name="comment" rows="3" cols="30"></textarea><br>

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

</form>

</body>
</html>

Output

Extracting  the data from the form in PHP

form.php

<?php
if (isset($_POST['submit'])){
    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];


echo  "<b> Hello </b>".$name;
echo"<br>";
    echo "<b> Your E-mail </b>". $email;
    echo"<br>";
    echo $comment;
}
?>

<html>
<head>
<meta charset = "UTF-8">//It is used for enable Unicode format text in the web page.
    <title> This is demo </title>
</head>
<body>
<form action= "form.php" method= "post">
    Name: <input type="text" name="name"> <br>

    E-mail: <input type="text" name="email"><br>

    Comment: <textarea name="comment" rows="3" cols="30"></textarea><br>

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

</form>

</body>
</html>

Output
 

Use of valdation in PHP

form.php

<?php
if (isset($_POST['submit'])) {
//for form validation
    $minimum = 10;
    $maximum = 20;


    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    if(empty($name) || empty($email)|| empty($comment))
    {
        echo "All fields are mandatory.";
        echo "<br>";
    }


    if (strlen($comment) < $minimum) {
        echo "Comment should be minimum than 10 characters";
        echo "<br>";
    }
    if (strlen($comment) < $maximum) {
        echo "Comment should not be maximum than 20 characters";

    }

}
?>

<html>
<head>
<meta charset = "UTF-8">//It is used for enable Unicode format text in the web page.
    <title> This is demo </title>
</head>
<body>
<form action= "form.php" method= "post">
    Name: <input type="text" name="name"> <br>

    E-mail: <input type="text" name="email"><br>

    Comment: <textarea name="comment" rows="3" cols="30"></textarea><br>

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

</form>

</body>
</html>

Output

                     Or                             

External page submission form in PHP

We use the external page for the code cleaning so that any one can easily understand the code and for that use make the two pages 

1. First page form.php. It is a view page all the  HTML coding is done here only
2. Second page form_process.php. It is used for business logics and sending response in the form all the PHP code is done here.
3. In form action we use the page url .example :- <form action= "form_process.php" method= "post"> to process the form.

form.php

<html>
<head>
<meta charset = "UTF-8">//It is used for enable Unicode format text in the web page.
    <title> This is demo </title>
</head>
<body>
<form action= "form_process.php" method= "post">
    Name: <input type="text" name="name"> <br>

    E-mail: <input type="text" name="email"><br>

    Comment: <textarea name="comment" rows="3" cols="30"></textarea><br>

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

</form>

</body>
</html>

Output

form_process.php

<?php
if (isset($_POST['submit'])) {
//for form validation
    $minimum = 10;
    $maximum = 20;


    $name = $_POST['name'];
    $email = $_POST['email'];
    $comment = $_POST['comment'];
    if(empty($name) || empty($email)|| empty($comment))
    {
        echo "All fields are mandatory.";
        echo "<br>";
    }


    if (strlen($comment) < $minimum) {
        echo "Comment should be minimum than 10 characters";
        echo "<br>";
    }
    if (strlen($comment) < $maximum) {
        echo "Comment should not be maximum than 20 characters";

    }

}
?>

 Output

 

 



Keywords: