开发者

Problems with Comparing Database Values and Text Input using MySQL and jQuery - Undefined Index and mysql_fetch_assoc() Errors

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\lifesearch\requires\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).removeCla开发者_开发技巧ss("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!


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜