How to Create a PHP Form Validation (isset and empty function)

in #utopian-io7 years ago (edited)

What Will I Learn?

  • You will learn how to create form validation
  • You will learn how make your database more valid
  • you will learn how to make secure website

Requirements

  • you must understand PHP basic programming
  • you must know how to implement isset and empty function
  • you must understand how to create form structure

Difficulty

  • Intermediate

Tutorial Contents

validation process is needed on a website form, function so that the data inputted into the database of your website becomes more valid, would make it easier if you want to create a report data from your website because there is no wrong data format.

  • Okay, just start our tutorial.
  • first you have to open the text editor, i suggest you to use notepad ++ as i use so this tutorial is easy to understand.
    image.png
  • then create two files, form.php file and warning.php file
  • form.php file serves as the form where we fill in the data, and warning.php file serves as a warning if the form is not filled correctly.
  • type the script below and intersect with the name form.php
<!DOCTYPE html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>utopian-io tutorial</title>
</head>
<body>
<h2>how to create validation form </h2>
<form action="proses.php" method="get">
NAME: <input type="text" name="nama" />
<br/>
E-Mail: <input type="text" name="email" />
<br/>
<input type="submit" value="Proses Data" >
</form>
</body>
</html>
  • I will explain a bit about the script above
  • This script serves as an account to the warning.php page
    <form action="warning.php" method="get">
  • then for the design of the form we use the script below
<form action="warning.php" method="get">
NAME: <input type="text" name="nama" />
<br/>
E-Mail: <input type="text" name="email" />
<br/>
<input type="submit" value="Proses Data" >
</form>
  • then we will create a warning page, type the script below and save it with the name warning.php
<?php
if (isset($_GET['name']) AND isset($_GET['email']))
{
$nama=$_GET['name'];
$email=$_GET['email'];
}
else
{
die("sorry, you must acces this page from form.html page");
}
  
if(empty($name))
{
die("sorry, you must enter your name");
}
else
{
if (is_numeric($name))
{
die("sorry, name must containing alphabet");
}
else
{
echo "Nama: $name <br /> Email: $email";
}
}
?>
  • I will explain about the script above
  • this serves as a warning if the form is not filled
if(empty($name))
{
die("sorry, you must enter your name");
}
  • then this as a warning if the variable name has no letter character in it
if (is_numeric($name))
{
die("sorry, name must containing alphabet");
}
  • This is a script that serves to display a warning when we access the file warning.php without entering the form page first.
else
{
die("sorry, you must acces this page from form.html page");
}
  • now we try to run the form we have created, whether it is working like yag we want.
  • run the file form.html by typing the filename in your browser search field as shown below.
    image.png
    the form already appears

  • now to try it we will hit the submit button without filling the data first we see whether to perform warning.

    image.png

    error message

  • then we try to fill in the name with a number character.
    image.png

    then press submit button

  • Let's see what will come up
    image.png

    error format message

  • now we try to run the file warning.php without accessing the file form.html first. we'll see what will come up.
    image.png

    warning that we access the file form.html first

  • okay, all warnings have been successful when there is an error, meaning we have successfully mebuat a form with more valid data.

Curriculum



Posted on Utopian.io - Rewarding Open Source Contributors

Sort:  

Your contribution cannot be approved because it is not as informative as other contributions. See the Utopian Rules. Contributions need to be informative and descriptive in order to help readers and developers understand them.

Suggestions:

  • You should give more information about provided code examples. Instead of just pasting code and telling readers what are they for, you should really teach technical aspects about the project you contribute.
  • And also outdated information or examples which may cause a vulnerability should not be taught without warnings, like SQL Injection in this case.
  • I also think that form validation is a data standardization method rather than a security method. So, I see no point for writing "you will learn how to make secure website
    ".

You can contact us on Discord.
[utopian-moderator]

Hey @roj, I just gave you a tip for your hard work on moderation. Upvote this comment to support the utopian moderators and increase your future rewards!

it's very detail tutorial, thank @lapulga don't forget to follow me back

Please inform rookies that this is NOT by any means protecting you from, for example, SQL Injection.

oke, thank you for the comment

it's very detail tutorial, thank @lapulga don't forget to follow me back

great tutorial. I think this very usefull for who new with php

thank you @sogata, i hope i'll be like you

Coin Marketplace

STEEM 0.20
TRX 0.15
JST 0.030
BTC 65074.63
ETH 2635.72
USDT 1.00
SBD 2.83