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 在编辑之前,我得到索引未定义的错误,虽然在 queries.php 页面上,我可以看到 search 的内容,但它仍然显示为错误并且没有' 将其提供给处理脚本:

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

PHP 编辑后,出现变量未定义错误:

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

编辑:

添加更多代码: https://pastebin.com/XcKPvWvb queries.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

I changed method from GET to POST, also changed the name of form,and the search button

<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']);
  }
}