How Do I Build A Multiple Choice Search Form?

A

Anonymous

Guest
Using the expertise given in the following thread...

http://www.php-forum.com/p/viewtopic.php?t=5937&sid=d8c7f0d89f03a74c0cf6a3a9e66e9383

I would like to build a multiple choice search form with an input box to search a MySql db. The user would select from a number of check boxes and after selecting the check boxes the user would fill in a search term in the input box, click OK, and then the requested search term would be found in the db. I do not know how to process the check box input if the boxes are checked. The selections and input box data would be sent via an html string to the php page where input is taken, a connection is made to the MySql db, and results are printed out.

The db has four fields. Source, Topic, Subtopic, References. I want to give the user the option of selecting from six choices for the source and then give the user an input box to select from Topic or Subtopic.

I am a novice so please be profuse in your comments.

Thank you in advance for any replies.
 
the checkboxes should be something like
Code:
<input type="checkbox" name="topic[]" value="1" />Topic 1
<input type="checkbox" name="topic[]" value="2" />Topic 2
<input type="checkbox" name="topic[]" value="3" />Topic 3

and in php....

Code:
$topics = implode(', ', $_POST['topic']);
$sql = "select * from table_name where topic in ($topics)";
..
 
ruturajv, thank you for the reply. I am a novice. Note that in my request i said I had a form with BOTH checkboxes and an input box. Here is the html for the form.

<p> </p>
<form method="GET" action="Topic.php">
<p><b><font face="Arial" size="4">Select Source</font></b></p>
<p><font size="2" face="Arial"><b>TR <input type="checkbox" name="TR" value="ON">   
ES <input type="checkbox" name="ES" value="ON">   NV <input type="checkbox" name="NV" value="ON">  
TH <input type="checkbox" name="TH" value="ON">   SM <input type="checkbox" name="SM" value="ON">  
EN <input type="checkbox" name="EN" value="ON"> ALL <input type="checkbox" name="ALL" value="ON"></b></font></p>
<p><b><font face="Arial" size="4">Search Topics</font></b></p>
<p><input type="text" input name="SeeAlso" size="20"><input type="submit"><input type="reset" value="Reset" name="B2"></p>
</form>
<p> </p>

Here is my current code on the top of the results page. The current results page is now called from a form with only an input box and no checkboxes.

$username = "UserNew";
$password = "PassNew";
$dbname = "nwe222_confind1";
$global_db = mysql_connect('localhost', $username, $password);
mysql_select_db($dbname, $global_db);

$query = "SELECT * FROM `Table1' WHERE `Topic` REGEXP '[[:<:]]($SeeAlso)[[:>:]]'";

$result = mysql_query($query) or die("ERROR63");

Can you help me translate your suggestion using implode or whatever will work, into what I am currently using? I do not know how to integrate BOTH the input box AND the checkbox selections into the SELECT statement. If the user selects one, two, three, four, five, or ALL checkboxes, I want the results to show only those records with the source equal to the source selected by the checkboxes AND with the Topic equal to what the user typed in the input box.
 
ES <input type="checkbox" name="topic[]" value="ES">  
NV <input type="checkbox" name="topic[]" value="NV">  
TH <input type="checkbox" name="topic[]" value="TH">  
SM <input type="checkbox" name="topic[]" value="SM">  
...

considering that is your topic id

you do

//my script i gave before...

select * from table_name
where topic in... $topics
 
I posted the following lines in a php page and got the following error message.

ES <input type="checkbox" name="topic[]" value="ES">  
NV <input type="checkbox" name="topic[]" value="NV">  
TH <input type="checkbox" name="topic[]" value="TH">  
SM <input type="checkbox" name="topic[]" value="SM">  

Parse error: parse error, unexpected '<' in /home/codeonel/public_html/XR/PageOne.php on line 35
 
OK, let's ramp this up a bit.

I get the following input into my php results page from the checkboxes and the input box on my html form. There are seven checkboxes and one input box.

Array ( [TR] => ON [ES] => ON [NV] => ON [TH] => ON [SM] => ON [EN] => ON [ALL] => ON [SeeAlso] =>AAA )

//I can convert the input from the checkboxes to a string variable in
//the following code

$ES = $_GET['ES'];
$NV = $_GET['NV'];
$TR = $_GET['TR'];
$TH = $_GET['TH'];
$SM = $_GET['SM'];
$EN = $_GET['EN'];
$ALL = $_GET['ALL'];
$SeeAlso = $_GET['SeeAlso'];

QUESTION: If a user checks on two or more checkboxes, those values would have to be combined in the SELECT statement. The issue is how do I put the variables into the select statement in variable form. How do I get all the string values in the variables above into my select statement?
 
Back
Top