簡體   English   中英

PHP表單索引未定義和變量未定義

[英]PHP Form index undefined and variable undefined

一直在做一個簡單的搜索,但是我遇到了一個問題。 我在_POST請求中收到索引未定義和變量未定義的錯誤! 我真的找不到錯誤,有人可以幫助我嗎? 順便說一下,該項目是在3個不同的文件上完成的。

HTML:

<form action="ajax/queries.php" method="POST" class="searchbox sbx-google">
  <div role="search" class="sbx-google__wrapper">
    <input type="text" name="search" placeholder="Įveskite paieškos terminus" autocomplete="off" required="required" class="sbx-google__input">
    <button type="submit" title="Submit your search query." class="sbx-google__submit">
      <svg role="img" aria-label="Search">
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sbx-icon-search-13"></use>
      </svg>
    </button>
    <button type="reset" title="Clear the search query." class="sbx-google__reset">
      <svg role="img" aria-label="Reset">
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sbx-icon-clear-3"></use>
      </svg>
    </button>
  </div>
</form>
<script type="text/javascript">
  document.querySelector('.searchbox [type="reset"]').addEventListener('click', function() {  this.parentNode.querySelector('input').focus();});
</script>

PHP在編輯之前,出現索引未定義的錯誤,盡管在querys.php頁面上,我可以看到search的內容,盡管它仍然顯示為錯誤,並且不提供給處理腳本:

global $query;
//
$query = htmlspecialchars($_POST['search']);  

PHP編輯后,出現變量未定義錯誤:

//Query
if (!isset($_POST)) {
  if (!isset($_POST["search"])){
    $query = htmlspecialchars($_POST['search']);
  }
} 

編輯:

添加更多代碼: https : //pastebin.com/XcKPvWvbquery.php https://pastebin.com/v7cL6Jw5 paieska.php(查詢未提供) pastebin dot com slash jh5wLPLR index.php(html)

PHP:您一直在以錯誤的順序執行if語句。 請嘗試這樣的if語句:

// Check if the post variable is set:
if (isset($_POST)) {
// Check if the key search post variable is set:
  if (isset($_POST["search"])){
    // Define the query:
    $query = htmlspecialchars($_POST["search"]);
  }
} 

HTML:我看不到已經分配了輸入的名稱,請確保您執行了此操作,這樣輸入將以鍵作為輸入的名稱發布。

如果我可以進一步幫助您,請告訴我。

刪除! 在兩個!isset()

在您的表單上,您為表單,輸入字段以及按鈕造成了混淆, name="search"

更新的HTML

我將方法從GET更改為POST,還更改了表單名稱和搜索按鈕

<form action="ajax/queries.php" method="POST" name="search_form" class="searchbox sbx-google">
  <div role="search" class="sbx-google__wrapper">
    <input type="text" name="search" placeholder="Įveskite paieškos terminus" autocomplete="off" required="required" class="sbx-google__input">
    <button type="submit" title="Submit your search query." name="search_button"  class="sbx-google__submit">
      <svg role="img" aria-label="Search">
        <use xmlns:xlink="http://www.w3.org/1999/xlink" xlink:href="#sbx-icon-search-13"></use>
      </svg>
    </button>

只是一個小錯誤..每個if(isset..你曾使用條件!(不)操作員

更新了PHP代碼

if (isset($_POST['search_button'])) {
  if (isset($_POST["search"])){
    $query = htmlspecialchars($_POST['search']);
  }
} 

暫無
暫無

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

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