I'm rather new at this myself, but I have gotten some good help here, so let me try to offer you some help.
I have a working search below for you that searches a MySQL database. (I don't think you said what database you are using, but I'm guessing it's MySQL.) Of course, the answer is in two parts. Below, you'll find code for a PHP search of a MySQL database. It's pretty easy to follow, just fill in the places where you have to by following the name, like YourDataBaseNameHere or YourTableNameHere.
This is the PHP side of things that searches and retrieves the data from the HTML form, and it includes a small bit of HTML code, including some error checks. Call it "Search.php":
- - -
<html>
<head>
<title>Search Results for MySQL Database</title>
</head>
<body>
<h2>Search Result</h2>
<?
if (!$searchtype || !$searchterm)
{
echo "You have not entered search details. Please go back and try again.";
exit;
}
if (!$db)
{
echo "Error: Could not connect to database. Please try again.";
exit;
}
mysql_select_db("YourDataBaseNameHere");
$query = "select * from YourTableNameHere where ".$searchtype." like '%".$searchterm."%'";
$result = mysql_query($query);
<h3><strong>Some Message Here At Bottom of PHP Search Page</strong></h3>
<br>
</body>
</html>
- - -
Then you need the HTML form, where one inputs a name to search for. Something like this:
<HTML>
<HEAD>
<TITLE>HTML Form for Search Page</TITLE>
</HEAD>
<BODY>
<br>
<form action="search.php" method="post">
<b>Choose a Search Category From the Pull-Down Menu:</b></font><br>
<br>
<select name="searchtype">
<option value="OrganizationNameField">Organization Name
<option value="CityField">City
<option value="CountryField">County
</select>
<br>
<br>
Enter the Search Term in the Window<br>
<input name="searchterm" type=text>
<br>
<input type=submit value="Search">
</form><br>
<br>
</font>
</BODY>
</HTML>
- - -
I hope I got it all here. Of course, you can add whatever fields you want to the pull-down menu. I hope this helps. Best of luck.
Would you be able to tell me how I could add a checkbox on my form side, allowing choice of a "Country" or some other field, to modify the search using the code above?
I'm trying to put on a checkbox to limit searches, but I'm not sure how I would get that to function using the code I have, what I would add to it on the PHP side, or actually even on the form side. Any help appreciated.
I'm talking about the code that is above. It works by a pull-down menu to select a topic and a word is input into a search form. I'm wondering how I might be able to have it take input from a checkbox that limits the search, either in addition or in place of the pull-down menu.
To wit, you check a box for, say, "Country," and then type a name. It returns the list of names within that country.
Thanks again. I have one more question, if you don't mind.
When I try to get input from a checkbox into a MySQL database, it doesn't seem to accept it. I'm not positive the problem is with the checkboxes, but I suspect that it might be.
My SQL translator, which is great, creates a tinyint(3) field in MySQL from an MS Access checkbox. I have a bunch of separate checkboxes taking input. What gets put inside this tinyint(3) field, 0 or 1?
I created the form-side HTML for each checkbox, but is there something I'm not getting here? The field name is Age in MySQL.
I have a search page that basically does the same thing as the ones shown above, but I would like users to be able to select multiple categories from a menu when they search.
Is there any way to do this using the above code as an example?