djvk87
03-30-2011, 02:04 AM
Hi, I have a PHP page with a search input form that dynamically loads a results page through Dynamic Ajax Content (script from here (http://www.dynamicdrive.com/dynamicindex17/ajaxcontent.htm)) where a MySQL database is searched for the search variable from the form and results displayed as tabular data. The 2 pages source code can be viewed below.
search.php
<img src="/common/images/yanmar/spareparts/YanmarSpareParts.png" align="center">
<br />Please use the below form to search for the Yanmar Part you're looking for.<br />
<br />
<form name="yspsearch" action="javascript:ajaxpage('/yanmar/ajaxpages/searchresults.php',%20'contentarea')" method="get">
Search: <input type="text" name="term" />
<input type="submit" name="Submit" value="Search" />
</form>
searchresults.php
<?php
// Get the search variable from URL
$var = @$_GET['term'];
$trimmed = trim($var); // Trim whitespace from the stored variable
// Check for an empty string and display message
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// Check for a search parameter
if (!isset($var))
{
echo "<p>We don't seem to have a search parameter!</p>";
exit;
}
// Connect to database
mysql_connect ("host","username","password"); //(host,username,password)
// Specify database
mysql_select_db ("host_database") or die(mysql_error());
// Build SQL Query
$query = "select * from database where Heading like \"%$term%\" or SubHeading like \"%$term%\" or Description like \"%$term%\" or PartNo like \"%$term%\"
order by Heading";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, display message
if ($numrows == 0)
{
echo "<h1>Search Results</h1>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
exit;
}
// Next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// Get Results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die(mysql_error());
// Display what the person searched for
echo "<p>You searched for: "" . $var . "$quot;</p>";
// Begin to show results
echo "<h1>Search Results</h1>";
// Show Results in table
while($row= mysql_fetch_array( $result)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"".$row['Image']."\">";
echo "</td><td>";
echo $row['SubHeading'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['PartNo'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
}
echo "</table>";
The problem I am having is the search variable is not passing to the dynamically loaded page and so it always displays "Please enter a search...".
What am I doing wrong here?
A working online example can be found by clicking the search button at the top left of this page (http://woodenboatshop.com.au/yanmar/spareparts.php).
Thanks
search.php
<img src="/common/images/yanmar/spareparts/YanmarSpareParts.png" align="center">
<br />Please use the below form to search for the Yanmar Part you're looking for.<br />
<br />
<form name="yspsearch" action="javascript:ajaxpage('/yanmar/ajaxpages/searchresults.php',%20'contentarea')" method="get">
Search: <input type="text" name="term" />
<input type="submit" name="Submit" value="Search" />
</form>
searchresults.php
<?php
// Get the search variable from URL
$var = @$_GET['term'];
$trimmed = trim($var); // Trim whitespace from the stored variable
// Check for an empty string and display message
if ($trimmed == "")
{
echo "<p>Please enter a search...</p>";
exit;
}
// Check for a search parameter
if (!isset($var))
{
echo "<p>We don't seem to have a search parameter!</p>";
exit;
}
// Connect to database
mysql_connect ("host","username","password"); //(host,username,password)
// Specify database
mysql_select_db ("host_database") or die(mysql_error());
// Build SQL Query
$query = "select * from database where Heading like \"%$term%\" or SubHeading like \"%$term%\" or Description like \"%$term%\" or PartNo like \"%$term%\"
order by Heading";
$numresults=mysql_query($query);
$numrows=mysql_num_rows($numresults);
// If we have no results, display message
if ($numrows == 0)
{
echo "<h1>Search Results</h1>";
echo "<p>Sorry, your search: "" . $trimmed . "" returned zero results</p>";
exit;
}
// Next determine if s has been passed to script, if not use 0
if (empty($s)) {
$s=0;
}
// Get Results
$query .= " limit $s,$limit";
$result = mysql_query($query) or die(mysql_error());
// Display what the person searched for
echo "<p>You searched for: "" . $var . "$quot;</p>";
// Begin to show results
echo "<h1>Search Results</h1>";
// Show Results in table
while($row= mysql_fetch_array( $result)) {
// Print out the contents of each row into a table
echo "<tr><td>";
echo "<img src=\"".$row['Image']."\">";
echo "</td><td>";
echo $row['SubHeading'];
echo "</td><td>";
echo $row['Description'];
echo "</td><td>";
echo $row['PartNo'];
echo "</td><td>";
echo $row['Price'];
echo "</td></tr>";
}
echo "</table>";
The problem I am having is the search variable is not passing to the dynamically loaded page and so it always displays "Please enter a search...".
What am I doing wrong here?
A working online example can be found by clicking the search button at the top left of this page (http://woodenboatshop.com.au/yanmar/spareparts.php).
Thanks