简体   繁体   中英

How to display error message?

  <!DOCTYPE html>
      <html>
  <body>

  <?php

if (isset($_POST['txt'])) {

    $veggies = array("Potato", "Cucumber", "Carrot", "Orange", "Green Beans", "onion");
    $fruits  = array("Apple", "Banana", "orange", "Pineapple", "Grapes", "Watermelon");
    $salad   = array_merge ($veggies, $fruits);
    $Object = $_POST['txt'];
    $search = array_filter($salad, function($list) use ($Object) {
        return ( stripos($list, $Object) !== FALSE );
    });

      print_r($search);

         // echo '<pre>' . print_r($search) . '</pre>';
     } 

     else {
    echo " Nothing entered into the Search Item field ";
      }

   ?>

   <form method="POST"> 
     Search item:  <input type="text" name="txt" ><br> <br>
              <input type="submit">
  </form>

 </body>
 </html>

I have one small doubt, if i click submit button without entering any value in the text box it should show "Nothing entered into the Search Item field", instead its showing "Array()", how to fix this issue ??

From the manual:

A variable is considered empty if it does not exist or if its value equals FALSE. empty() does not generate a warning if the variable does not exist.

<?php

if($_SERVER['REQUEST_METHOD'] == 'POST')) {
    if(!empty($_POST['txt'])) {
        // Do your thing
    } 
    else {
        echo " Nothing entered into the Search Item field ";
    }
}

?>

Whereas with isset:

$foo = '';
var_dump(isset($foo));

Outputs:

bool(true)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM