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!