一直在做一个简单的搜索,但我遇到了一个问题。我在_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 在编辑之前,我收到索引未定义错误,尽管在查询.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 查询.phphttps://pastebin.com/v7cL6Jw5 paieska.php(查询不会提供给它) pastebin dot com slash jh5wLPLR
索引.php (html)
在您的表单上,您为表单、输入字段以及造成混淆的按钮提供了name="search"
更新的网页
我将方法从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']);
}
}
删除两个!isset()
中的!
。
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:我看不到已分配输入的名称,请确保您这样做,以便输入将以键作为输入的名称发布。
如果我能进一步帮助您,请告诉我。