A simple but Secure Html form with validations using PHP

Here is another simple but Secure Html form with validations using PHP. All you need to do is just copy this code below, paste it in your Notepadd++ or whatever editor you are using and save that file. Now put that file in your htdocs folder in xampp and you are good to go.

code:

<?php //php protion ?>

<?php

// Firstly I have defined the variables here and set them to empty values

$nam = $eml = $pass = "";
$namError = $emlError = $passError = "";

if($_SERVER["REQUEST_METHOD"] == "POST"){

   if(empty($_POST["name"])){
    $namError = "name is required";
   }
   else{
    $nam = test_input($_POST["name"]);
   }

   if(empty($_POST["email"])){
    $emlError = "Email is required";
   }
   else{
    $eml = test_input($_POST["email"]);
   }

   if (empty($_POST["password"])) {
    $passError = "Password is required";
   }
   else{
    $pass = test_input($_POST["password"]);
   }
}

function test_input($data){
$data = trim($data);
$data = stripcslashes($data);
$data = htmlspecialchars($data);
return $data;
}

?>

<?php //html portion ?>

<!DOCTYPE HTML>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Simple html form by TALHA SHOAIB</title>
</head>
<body>

<h1>A simple but Secure Html form with validations using PHP | developed by Talha shoaib, available at <a href="https://techkwave.blogspot.com/">Tech wave</a></h1>

        <span style="color: red;">* Required feilds</span>
        <form action=" <?php echo htmlspecialchars($_SERVER["PHP_SELF"]) ?> " method="post">
            <input type="text" name="name" placeholder="Enter your name">
            <span style="color: red;">* <?php echo $namError; ?> </span>
            <br>
            <br>
            <input type="email" name="email" placeholder="Enter your email">
            <span style="color: red;">* <?php echo $emlError; ?> </span>
            <br>
            <br>
            <input type="password" name="password" placeholder="Password">
            <span style="color: red;">* <?php echo $passError; ?> </span>
            <br>
            <br>
            <input type="submit" name="btn" value="Button">
        </form>

        <?php
          echo "<h1>Output:</h1><br><br>";

          echo "Your name: $nam <br>";
          echo "Your Email: $eml <br>";
          echo "Your Password: $pass";
        ?>
</body>
</html>



if you are facing any problem, then let me know in the comment section and let’s solve it together.
Enjoy

Result screen

Share it in your network:

Leave a Comment

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.

small_c_popup.png

Congratulations!

You have successfully subscribed to my email list. Now you will be recieving latest wordpress tips and tricks directly in your mail box.

Don’t worry! I won’t spam you