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)
3
Answers
PHP: You’ve been doing your if statement in the wrong order. Please try your if statement like this:
HTML: I can’t see that have assigned the name of the input, please make sure you do this so the input will be posted with the key as the name of the input.
If I can help you any further, please let me know.
Remove the
!
in both!isset()
s.On your form, you gave the
name="search"
for the form, input field, and also for the button which made an confusionUpdated Html
Just a small mistake.. on each
if(isset..
conditions you had used ! (not) OperatorUpdated PHP code