Search code examples
javascriptphphtmlebay-api

PHP Form index undefined and variable undefined


been doing a simple search, but I have come in to a problem. I get an error of index undefined and variable undefined on my _POST request! I can't really find the mistake, could anyone help me? The project is done on 3 different files by the way.

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 before editing, I get index undefined error, though on queries.php page, I can see the contents of search, though it still shows off as an error and doens't supply it to the processing script:

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

PHP after editing, I get variable undefined error:

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

EDIT:

adding some more code: https://pastebin.com/XcKPvWvb queries.php https://pastebin.com/v7cL6Jw5 paieska.php (query doesn't get supplied to it) pastebin dot com slash jh5wLPLR index.php (html)


Solution

  • On your form, you gave the name="search" for the form, input field, and also for the button which made an confusion

    Updated 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>
    

    Just a small mistake.. on each if(isset.. conditions you had used ! (not) Operator

    Updated PHP code

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