簡體   English   中英

如何顯示錯誤信息?

[英]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>

我有一個小小的疑問,如果我在文本框中未輸入任何值的情況下單擊“提交”按鈕,則應顯示“沒有在搜索項字段中輸入任何內容”,而是顯示“ Array()”,如何解決此問題?

從手冊中:

如果變量不存在或其值等於FALSE,則將其視為空。 如果變量不存在,則empty()不會生成警告。

<?php

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

?>

而isset:

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

輸出:

bool(true)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM