PHP check if variable is empty is an important task when using form. It is a check step so that input data is delivered to the next steps so that server can avoid making irrelevant things or wrong results. Checking whether data is empty or delivered or not is the job of server but not the clients. Therefore, code is processed by using PHP instead of HTML5 or Javascrip.

You may know a little bit about how to use form so in this article we do not mention about basic knowledge of form but focus on PHP check if variable is empty.

As far as we know, depending on the method of form, there are different ways of retrieval. If you use Post method, form’s retrieval syntax is $_POST[‘input name’], for instance:

<form method=”POST” action=”ok.php” >
<input name=”usname” type=”text” />
<input name=”submit” value=”submit”>
</form>

In order to get data of usname field, we use $usname = $_POST[‘usname’] in file ok.php. In the above example, the name of input field is usname.

Similarly, when method=GET, instead of using $_POST, we use $_GET[‘input name’]

<form method=”GET” action=”ok.php” >
<input name=”usname” type=”text” />
<input name=”submit” value=”submit”>
</form>

Now we get by using $usname = $_GET[‘usname’]
There is another way of retrieval that we do not mind the method is using $_SEVER[‘input name’]
In both two above cases, collecting data by using $_SEVER[‘usname’] is okey.
Via these above examples, we have known how to get data from form. Now we only need to check whether data is empty or not.
The simplest task of PHP check if variable is empty is comparing the entered data with empty data.
Comparing syntax used in PHP is “==” and “!=”
Combining with IF ELSE, we have two solutions to this problem. The following example inherit the using form previously.

<form method=”POST” action=”ok.php” >
<input name=”usname” type=”text” />
<input name=”submit” value=”submit”>
</form>
And getting data
$usname = $_POST[‘usname’]

Solution 1: Check if data equal to empty data, data is empty.

If ($usname ==””){echo “data is empty”;}
Solution 2: Check if the data is not empty, in the opposite case, data is empty

If ($usname !=””){echo “data is not empty”;}

Else { echo “data is empty”;}

CODE FULL

You are using your acquainted code editor. But in this article and others related to code, I use PHP Designer and Notepad ++ code editor. Moreover, xampp is used as virtual server to install in personal computer. Basic code is showed as previous part so that you can imagine. Full code is also added in order to nicer css page.

Firstly, you create checkform folder in XAMPP\htdocs folder

In checkform folder, you create file index.html with following content:

<!DOCTYPE HTML>
<html>
<head>
<meta http-equiv="content-type" content="text/html" />
<meta name="author" content="TRAN DINH HONG" />
<title>php check if variable is empty</title>
</head> 
<body>
<form action="ok.php" method="post">
<input name="usname" type="text" />
<input name="submit" type="submit" value="submit" />
</form>
</body>
<style>
body {
width: 300px;
margin: auto;
border: 1px solid #cdcdcd;
padding: 80px 15px;
margin-top: 200px;
}
</style>
</html>

In checkform folder, you continuously create file ok.php with following content

<?php
/**
* @author TRAN DINH HONG
* @copyright 2018
*/
$usename = $_POST['usname'];
if ($usename ==''){
echo 'data is empty';
}
else {
echo 'data is '.$usename ;
}
?>
<style>
body {
width: 300px;
margin: auto;
border: 1px solid #cdcdcd;
padding: 80px 15px;
margin-top: 200px;
height: 300px;
box-sizing: border-box;
}
</style>  

You can learn the instruction in this below video or download full code for this project.

 

Download code : PHP-check-if-variable-is-empty

If you are using shopify, you do not have to undestand every line of code because it has an available app Powerful form builder  that supports for free to create form simply, add many fields and data types.

 

Leave a Reply

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