...

View Full Version : AJAX MYSQL Search



christianbroger
07-26-2010, 05:56 PM
I am currently trying to put together a AJAX search that searches a MySQL database using PHP. I have created the below scripts. I cannot get it to function properly. Essentially I want someone to type in a partial search term and click submit and see the results. The purpose of this is for it to become part of a mobile app. Any help would be appreciated. Thank you!

HTML:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>


<style type="text/css">
body{font-family:'Lucida Grande', Verdana, sans-serif;; font-size:14px; color:#666666;}
h2{color:#000000; margin-bottom:20px;}
h3{color:#000000; font-size:14px;}
input{font-size:16px; color:#444444;}
a:link, a:visited, a:hover{color:#0033CC;}
a:hover{text-decoration:none;}
div.searchInput{padding:8px; background:#DEDEDE; clear:both;}
div.footer{padding:6px; border-top:solid 1px #DEDEDE; font-size:10px;}
#msg{background:#FFFFCC; margin-bottom:10px; padding:4px; display:none;}
</style>

<script type="text/javascript">

function showProducts(str)
{
if (str=="")
* {
* document.getElementById("search-result").innerHTML="";
* return;
* }
if (window.XMLHttpRequest)
* {// code for IE7+, Firefox, Chrome, Opera, Safari
* xmlhttp=new XMLHttpRequest();
* }
else
* {// code for IE6, IE5
* xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
* }
xmlhttp.onreadystatechange=function()
* {
* if (xmlhttp.readyState==4 && xmlhttp.status==200)
*** {
*** document.getElementById("search-result").innerHTML=xmlhttp.responseText;
*** }
* }
xmlhttp.open("GET","search.php?q="+str,true);
xmlhttp.send();
}
</script>


</head>

<body>

<form id="searchForm" name="searchForm" method="post" action="javascript:searchNameq();">
<div class="searchInput">
<input name="searchq" type="text" id="searchq" size="30" onkeyup="javascript:showProducts()"/>
<input type="button" name="submitSearch" id="submitSearch" value="Search" onclick="javascript:showProducts()"/>
</div>
</form>

<h3>Search Results</h3>
<div id="msg">Type something into the input field</div>
<div id="search-result"></div>

</body>

</html>


PHP:
<?php

$q=$_GET["q"];

//connect to the database
mysql_connect("christianbrogers.db.5646816.hostedresource.com","christianbrogers","GHbn123");
mysql_select_db("christianbrogers");

//explode our search term into separate words
$search_exploded = explode(" ",$search);

foreach($search_exploded as $search_each)
{

//construct the query
$x++;
if ($x==1)
$construct .= "name LIKE '%$search_each%'";
else
$construct .= " OR name LIKE '%$search_each%'";
}


//echo out the constructed query

$construct = "SELECT * FROM products WHERE $construct";

$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{

while ($runrows = mysql_fetch_assoc($run))
{

//get data
$name = $runrows['name'];

echo $name;
?>

RandomUser531
07-26-2010, 06:26 PM
I am currently trying to put together a AJAX search that searches a MySQL database using PHP. I have created the below scripts. I cannot get it to function properly.When you post code it should be enclosed in
tags.
Are you saying that your PHP isn't receiving the request or that your client script isn't returning the data that your PHP generates?

christianbroger
07-28-2010, 03:32 PM
The AJAX is talking to PHP but is not returning

disastro
07-28-2010, 03:45 PM
You need to define $construct before your for loop.



$construct = '';

foreach($search_exploded as $search_each)
{

//construct the query
$x++;
if ($x==1)
$construct .= "name LIKE '%$search_each%'";
else
$construct .= " OR name LIKE '%$search_each%'";
}


You were trying to concat an undefined variable.

christianbroger
08-04-2010, 02:30 PM
So I think I defined the construct (I'm fairly new to PHP). Below is my PHP code.

PHP:


(Connect to Database) //explode our search term into separate words
$search_exploded = explode(" ",$search);

$construct*=*'';

foreach($search_exploded as $search_each)
{

//construct the query
$x++;
if ($x==1)
$construct .= "name LIKE '%$search_each%'";
else
$construct .= " OR name LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "it LIKE '%$search_each%'";
else
$construct .= " OR it LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "description LIKE '%$search_each%'";
else
$construct .= " OR description LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";


}


//echo out the constructed query

$construct = "SELECT * FROM products WHERE $construct";

$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{

while ($runrows = mysql_fetch_assoc($run))
{

//get data
$name = $runrows['name'];
$it = $runrows['it'];
$desc = $runrows['description'];
$image = $runrows['image'];
$page = $runrows['page'];

echo "
<ul class='spageitem'>


<li class='stextbox'>
<span class='simage'>
<img src='../images/labelrite/$image'><br>
</span>


<span class='sheader'>
$name
</span>

<span class='stext'>
$it<br>
$desc<br>
<a href='$page'\"><img src='../images/interface/info.jpg' /></a>
</span><br>

</li>
</ul>
";
}
}
?>

I get the error: Parse error: syntax error, unexpected T_CONSTANT_ENCAPSED_STRING in /home/content/16/5646816/html/search.php on line 14

Line 14 is the $construct = '';

Before I did this, I could click search and see everything in my database but it was not discriminatory, based on what was typed in the field. Confused

hgs
08-04-2010, 03:14 PM
You call

onclick="javascript:showProducts()

however your function has a parameter str


function showProducts(str)

I assume you are sending an empty string over to the php script;

Next in the php script you say


$q=$_GET["q"];

to get the search string but you are not using $q anywhere in your script.

Should this

$search_exploded = explode(" ",$search);

not be


$search_exploded = explode(" ",$q);

????

christianbroger
08-06-2010, 03:53 PM
First of all, I really appreciate your help. Being knew at this, this is frustrating for me. Below is my code after the change. It is still loading the entire database.

HTML


<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>


<head>
<meta content="minimum-scale=1.0, width=device-width, maximum-scale=0.6667, user-scalable=no" name="viewport" />
<link href="../css/style.css" rel="stylesheet" type="text/css" />
<script src="../javascript/functions.js" type="text/javascript"></script>
<title>Kitchen iQ</title>
<link href="../images/interface/iphone/startup.png" rel="apple-touchstartup-image" />

<script type="text/javascript">
function showProducts(str)
{
if (str=="")
* {
* document.getElementById("search-result").innerHTML="";
* return;
* }
if (window.XMLHttpRequest)
* {// code for IE7+, Firefox, Chrome, Opera, Safari
* xmlhttp=new XMLHttpRequest();
* }
else
* {// code for IE6, IE5
* xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
* }
xmlhttp.onreadystatechange=function()
* {
* if (xmlhttp.readyState==4 && xmlhttp.status==200)
*** {
*** document.getElementById("search-result").innerHTML=xmlhttp.responseText;
*** }
* }
xmlhttp.open("GET","http://www.daymarkpoints.com/search.php?q="+str,true);
xmlhttp.send();
}
</script>

</head>

<body>
<div id="content">

<div class="navigation">
<a href="../index.html">Home</a>:
</div>

<ul class="pageitem">

<form id="searchForm" name="searchForm" method="get">
<div class="searchInput">
<input name="searchq" type="text" id="searchq" size="40"/>
<input type="button" name="submitSearch" id="submitSearch" value="Search" onclick="javascript:showProducts(this.value)"/>
</div>
</form>

<h3>Search results listed below.</h3>
<div id="search-result"></div>

</ul>

</body>

</html>




<?php

//initial variables from the form
$q = $_GET['q'];

//connect to the database
mysql_connect("XXXXXXXXXXXX","XXXXXXXXXX","XXXXXXXXX");
mysql_select_db("XXXXXXXXXXXXXX");

//explode our search term into separate words
$search_exploded = explode(" ",$q);

$construct = "SELECT * FROM products WHERE ";

{

//construct the query
$x++;
if ($x==1)
$construct .= "name LIKE '%$search_each%'";
else
$construct .= " OR name LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "it LIKE '%$search_each%'";
else
$construct .= " OR it LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "description LIKE '%$search_each%'";
else
$construct .= " OR description LIKE '%$search_each%'";

$x++;
if ($x==1)
$construct .= "keywords LIKE '%$search_each%'";
else
$construct .= " OR keywords LIKE '%$search_each%'";


}


//echo out the constructed query

$run = mysql_query($construct);

$foundnum = mysql_num_rows($run);

if ($foundnum==0)
echo "No results found.";
else
{

while ($runrows = mysql_fetch_assoc($run))
{

//get data
$name = $runrows['name'];
$it = $runrows['it'];
$desc = $runrows['description'];
$image = $runrows['image'];
$page = $runrows['page'];

echo "
<ul class='spageitem'>


<li class='stextbox'>
<span class='simage'>
<img src='../images/labelrite/$image'><br>
</span>


<span class='sheader'>
$name
</span>

<span class='stext'>
$it<br>
$desc<br>
<a href='$page'\"><img src='../images/interface/info.jpg' /></a>
</span><br>

</li>
</ul>
";
}
}
?>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum