使用带有$ _POST的条件

I have a one textbox. It name is example.

<input type="text" name="example"/>

I want to check, any data is coming from example or not. I tried use code as below:

<?php
if(empty($_POST["example"]))
{
      echo "You need to enter any text to example box.";
}
?>

But this code is printing,when I enter the page is first. I want to see printed data, only when click submit.

<?php
if(isset($_POST['example']) && empty($_POST['example']))
{
      echo "You need to enter any text to example box.";
}
?>

check for

if(!isset($_POST["submitButtonName"]))
{
   // validation logic here
} 

Its a better choice to use:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
    if(empty($_POST["example"]))
       {
          echo "You need to enter any text to example box.";
       }
}

This will check if there's a POST on server and then you will have your conditions.

if (!empty ($_POST))
{
    // Validation logic here
    if ((!isset ($_POST ['example'])) || (empty ($_POST ["example"])))
    {
        echo "You need to enter any text to example box.";
    }
}
else
{
    // No need to do any validation logic, the form hasn't been submitted yet. 
}

Check for the submit button first.

<input type="text" name="example" value="" />
<input type="submit" name="submit" value="submit" />

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

        if (empty($_POST['example'])) {
            echo "You need to enter any text to example box.";
        }
?>

isset is the proper choice here -- empty is only intended to examine a known variable to see if it is "emptyish". According to the docs

The following things are considered to be empty:

"" (an empty string)
0 (0 as an integer)
0.0 (0 as a float)
"0" (0 as a string)
NULL
FALSE
array() (an empty array)
var $var; (a variable declared, but without a value in a class)

What it doesn't say is how it treats array members that are not defined. A comment on the docs page gives us some insight from testing: http://www.php.net/manual/en/function.empty.php#105722

Note that checking the existence of a subkey of an array when that subkey does not exist but the parent does and is a string will return false for empty.

Whereas isset (docs) is designed to "Determine if a variable is set and is not NULL. " -- exactly what you're after. Thus, your code ends up looking like this:

// check the length, too, to avoid zero-length strings
if(!isset($_POST["example"]) || strlen(trim($_POST["example"])) < 1) {
     echo "You need to enter any text to example box.";
} else {
    // handle form submission
}

Documentation

PHP isset - http://php.net/manual/en/function.isset.php

PHP empty - http://www.php.net/manual/en/function.empty.php

More reading - http://virendrachandak.wordpress.com/2012/01/21/php-isset-vs-empty-vs-is_null/

Check $_POST variables first becuase it will be available only when page is submitted.

<?php
    if(!empty($_POST)){
      if(isset($_POST['example']) && empty($_POST['example'])){
         echo "You need to enter any text to example box.";
      }         
   }
 ?>