0

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.

7 Answers 7

3

check for

if(!isset($_POST["submitButtonName"]))
{
   // validation logic here
} 
Sign up to request clarification or add additional context in comments.

5 Comments

Why check for "submitButtonName" ? Is that a copy-paste error?
@JonathanM that is what happens when the submit button is clicked.
There's no evidence he's using a submit button. I make the same argument on ciriusrob's answer.
@JonathanM i quote: "... I want to see printed data, only when click submit"
The OP does say that a user clicks the submit button to submit the form, but this isn't always the case. The form will submit if the user presses Enter in the input, and in that case the submit button doesn't have to be included in the POST. If the submit button doesn't have a name then it's never included in the POST.
3

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/

Comments

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

1 Comment

Careful, 0 and "0" are considered empty. You may not want that.
0
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. 
}

2 Comments

This will disqualify a value of "O" or 0.
The OP's code would have disqualified 0 as well, unless he explicitly states that he wants 0 to pass then I'll have to go with the behaviour he implemented and hasn't indicated is wrong.
0

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.

1 Comment

This will disqualify a value of "O" or 0.
0

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.";
      }         
   }
 ?>

2 Comments

This will disqualify a value of "O" or 0.
Replace empty with strlen($_POST['example'])==0 And it will not disqualify value of "0"
-1

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.";
        }
?>

5 Comments

There may not be a submit button.
@JonathanM. What does this mean "I want to see printed data, only when click submit."?
He may be submitting via an image that looks like a button, and using ajax.
And which part of @Birlikisgu issue reads "I am using AJAX" or "I am using an image button"? Do you really understand?
Which part says he's got a submit button?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.