Solved

PHP/MYSQL/JAVA Search Box

Posted on 2012-03-29
1
321 Views
Last Modified: 2012-08-14
Hi, i have the below code working for my dynamic website search.
This works fine, however I'm having trouble implementing a message saying "No Result" when a user types a string that doesn't match anything in the database, like for example, the user types in the letter 'z' which doesn't match any titles.

Also, how would i implement a cache so the input doesn't keep calling the database every time a letter is pressed? Still trying to figure all this java stuff out...could someone please help me with this?!

function getSuggestions(value) {
	if(value != "") {
		$.post("/search.php", {userPart:value}, function(data) {
			$("#search_display").html(data);
		});
	} else {
		removeSuggestions();
	}
}
function removeSuggestions() {
	$("#search_display").html("");
}
function addText(value) {
	$("#search-box").val(value);
}

Open in new window


$userPart = mysql_real_escape_string(addslashes($_POST['userPart']));
$search_result = mysql_query("SELECT... WHERE subcat_name LIKE '%".$userPart."%' LIMIT 0, 16") or die (mysql_error());

while ($row_search_result = mysql_fetch_assoc($search_result)) {
	echo "<div class='link' onClick='addText(\"".$row_search_result['subcat_name']."\");'><a href='".$row_search_result['search_url']."'>".$row_search_result['subcat_name']."</a><br />in ".$row_search_result['cat_name']."</div>";
}

Open in new window

0
Comment
Question by:JiveMedia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 83

Accepted Solution

by:
Dave Baldwin earned 500 total points
ID: 37785474
Check the number of rows returned.

$userPart = mysql_real_escape_string(addslashes($_POST['userPart']));
$search_result = mysql_query("SELECT... WHERE subcat_name LIKE '%".$userPart."%' LIMIT 0, 16") or die (mysql_error());
$num_rows = mysql_num_rows($search_result);
if($num_rows == 0) echo "<div>No Results.</div>";
else {
while ($row_search_result = mysql_fetch_assoc($search_result)) {
	echo "<div class='link' onClick='addText(\"".$row_search_result['subcat_name']."\");'><a href='".$row_search_result['search_url']."'>".$row_search_result['subcat_name']."</a><br />in ".$row_search_result['cat_name']."</div>";
}
}

Open in new window

0

Featured Post

Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
This article discusses how to implement server side field validation and display customized error messages to the client.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…

738 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question