dongshai1944 2011-02-12 04:28
浏览 61
已采纳

使用MySQL和jQuery比较数据库值和文本输入的问题 - 未定义的索引和mysql_fetch_assoc()错误

I am having an issue with comparing text input with the values I have stored in a local database.

When I type in text into the text input field with id of #searchBar I get this error:

Notice: Undefined index: searchBit in C:\wamp\www\lifesearchequires\search.php on line 6

I have tried for a while now to fix this problem without any real success. I don't understand why it says that the index is undefined.

If anyone could offer a possible solution I would greatly appreciate it.

Here's the code:

search.js

function search(searchQuery) {
    $.post("requires/search.php", {searchBit:searchQuery}, function(data) {
        $("#searchResults").html(data);
    });
}

$(document).ready(function() {
    $("#searchBar").focus(function() {
        if ($("#searchBar").val() == "start searching...") {
            $("#searchBar").val("");
        }
        $(this).addClass("searchBarFocus");
        $(this).removeClass("searchBarBlur");
    });
    $("#searchBar").blur(function() {
        $(this).addClass("searchBarBlur");
        $(this).removeClass("searchBarFocus");
    });
    $("#searchBar").keypress(function() {
        $("#searchResults").fadeOut(200);
        $("#searchResults").fadeIn(200);
    });
    $("#search").mouseleave(function() {
        $("#searchResults").fadeOut(200);
    });
    $("#searchBar").keyup(search(this.val()));
});

searchBar.php

<div id="search">
    <form action="requires/search.php">
        <input type="text" value="start searching..." id="searchBar" class="searchBarBlur" 
        autocomplete="off"/>
    </form>
    <div id="searchResults">
    </div>
</div>

search.php

<?php
    $host = "localhost";
    $username = "root";
    $password = "";
    $database = "websites";
    $searchBit = $_POST['searchBit'];
    mysql_connect($host, $username, $password);
    mysql_select_db($database) or die("Failed to select table from database");
    $userSearch = mysql_real_escape_string(addslashes($searchBit));
    $searchResults = mysql_query("SELECT name FROM sites WHERE name LIKE '$userSearch%'");

    while ($row = mysql_fetch_assoc($searchResults)) {
        echo "<div class='searchResult'>".$row['name']."</div>";
    }
?>

Thanks in advance!

  • 写回答

1条回答 默认 最新

  • douyi3833 2011-02-12 04:46
    关注

    your form input field doesn't have a name attribute which should be "searchBit". $_POST['searchBit'] is undefined because of that..

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Llama如何调用shell或者Python
  • ¥20 eclipse连接sap后代码跑出来空白
  • ¥20 谁能帮我挨个解读这个php语言编的代码什么意思?
  • ¥15 win10权限管理,限制普通用户使用删除功能
  • ¥15 minnio内存占用过大,内存没被回收(Windows环境)
  • ¥65 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案