Hello, I was wondering what could be wrong? There is this ajax code that I'm trying to work on and its supposed to show the results in a div tag when submitting a search form, but theres no results.
here's my code
$(document).ready(function(){
$("form.ajax").submit(function(){
var ajax_div = $(this).attr("id")+"_results";
var data = $(this).serialize();
var url = $(this).attr("action");
var type = $(this).attr("method").toUpperCase();
$.ajax({
url: url,
data: data,
type: type,
success: function(data){
$("#"+ajax_div).html(data);
}
});
return false;
});
});
my form code
<form action="results.php" name="form" method="post" id="search" class="ajax" style="float:right;width:650px;height:60px;margin-right:30px;" onSubmit="return searchLocations();" >
<table style="float:right;"><tr>
<td>
<label for="testinput"><b>Search</b> </label><br />
</td>
<td>
<label for="testinput"><b>Location</b> </label><br />
</td></tr>
<tr>
<td>
<input name="search_term" id="search_term" type="text" style="height:23px; margin-right:20px;font-size: 16px" value="<?PHP echo $search_term; ?>" size="36" autocomplete="off"/></td>
<td>
<input name="addressInput" id="addressInput" type="text" style="height:23px;margin-right:10px;font-size: 16px" value="<?PHP echo $location_term; ?>" size="36" autocomplete="off"/></td><td>
<select id="radiusSelect">
<option value="25" selected>25</option>
<option value="100">100</option>
<option value="200">200</option>
</select>
</td><td>
<input type="submit" name="search_button" style= "padding: 5px; font-family: Arial, Helvetica, sans-serif; font-size: 12px;" value="Search" onClick="searchLocations();" />
</td></tr></table>
</form>
Here's my results page
<?php include("search_query.php");?>
<?PHP
if($results != 0)
{
?>
<table style="width:950px;"><tr><td style="float:left">Results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
<td style="float:right">Results <b>
<?PHP echo ($first_pos+1)." - ";
if(($RESULTS_LIMIT + $first_pos) < $results) echo ($RESULTS_LIMIT + $first_pos);
else echo $results ; ?>
</b>
out of <b><?PHP echo $results; ?></b>
for(<b><?PHP echo sprintf("%01.2f", $time_search); ?></b>)
seconds </td>
</tr>
</table>
<?php require("filter.php");?>
<table style="float:left;position:relative">
<?PHP
while($row = mysql_fetch_array($sql_result_query))
{
?>
<tr align="left">
<td colspan="2" style="padding-right:30px;"><?PHP echo $row['product']; ?></td>
<td colspan="2"><?PHP echo $row['location']; ?></td>
</tr>
<?PHP
}
?>
</table>
<?PHP
}
elseif(isset($sql_query))
{
?>
<table border="0" cellspacing="2" cellpadding="0">
<tr>
<td align="center">No results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
</tr>
</table>
<?PHP
}
?>
<div style="float:left;position:relative;">
<?php require("includes/pagination.php");?>
</div>
with my search query php code
<?PHP
global $search_term;
global $location_term;
global $results;
function getmicrotime()
{
list($usec, $sec) = explode(" ", microtime());
return ((float)$usec + (float)$sec);
}
$connection_string = "connectionstring.php";
require_once($connection_string);
mysql_select_db("chef") or die ( 'Unable to select database.' );
$RESULTS_LIMIT=5;
if(isset($_GET['search_term']) && isset($_GET['addressInput']) && isset($_GET['search_button']))
{
$search_term = $_GET['search_term'];
$location_term = $_GET['addressInput'];
if(isset($_GET['first_pos']))
{
$first_pos = $_GET['first_pos'];
}
else
{
$first_pos = 0;
}
$start_search = getmicrotime();
$sql_query = mysql_query("SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term')");
if($results = mysql_num_rows($sql_query) != 0)
{
$sql = "SELECT * FROM searchengine WHERE MATCH(product) AGAINST('$search_term') AND MATCH(location) AGAINST('$location_term') LIMIT $first_pos, $RESULTS_LIMIT";
$sql_result_query = mysql_query($sql);
}
else
{
$sql = "SELECT * FROM searchengine WHERE (product LIKE '%".mysql_real_escape_string($search_term)."%' AND location LIKE '%".mysql_real_escape_string($location_term)."%') ";
$sql_query = mysql_query($sql);
$results = mysql_num_rows($sql_query);
$sql_result_query = mysql_query("SELECT * FROM searchengine WHERE (product LIKE '%".$search_term."%' AND location LIKE '%".$location_term."%') LIMIT $first_pos, $RESULTS_LIMIT ");
}
$stop_search = getmicrotime();
$time_search = ($stop_search - $start_search);
}
?>
<?PHP
if($results != 0)
{
?>
<table style="width:950px;"><tr><td style="float:left">Results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
<td style="float:right">Results <b>
<?PHP echo ($first_pos+1)." - ";
if(($RESULTS_LIMIT + $first_pos) < $results) echo ($RESULTS_LIMIT + $first_pos);
else echo $results ; ?>
</b>
out of <b><?PHP echo $results; ?></b>
for(<b><?PHP echo sprintf("%01.2f", $time_search); ?></b>)
seconds </td>
</tr>
</table>
<?php require("filter.php");?>
<table style="float:left;position:relative">
<?PHP
while($row = mysql_fetch_array($sql_result_query))
{
?>
<tr align="left">
<td colspan="2" style="padding-right:30px;"><?PHP echo $row['product']; ?></td>
<td colspan="2"><?PHP echo $row['location']; ?></td>
</tr>
<?PHP
}
?>
</table>
<?PHP
}
elseif(isset($sql_query))
{
?>
<table border="0" cellspacing="2" cellpadding="0">
<tr>
<td align="center">No results for <?PHP echo "<i><b><font color=#000000>".$search_term."</font></b></i> "; ?></td>
</tr>
</table>
<?PHP
}
?>
<div style="float:left;position:relative;">
<!--require("includes/pagination.php")!-->
</div>
I know this is a lot of coding but I think whats giving me problems is the form code and the ajax.
I want to thank for those who are willing to help me out.