CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Is there a better way of doing search



Jun 13, 2000, 12:43 PM

Post #1 of 4 (895 views)
Is there a better way of doing search Can't Post

I want to do the search based on the form input. I have 4 different checkboxes. If the user selects all 4, or if he selects 2 or 1,i am writing for each occurence. Is there a better way of doing it. I am getting the value as on or off in a variable. How do you get only the checked one's that are selected and use them to select from the database.
Please help me

User / Moderator

Jun 13, 2000, 4:48 PM

Post #2 of 4 (895 views)
Re: Is there a better way of doing search [In reply to] Can't Post

Make sure all four checkboxes have the same name, then -- if you're using the CGI module -- you can access them all via param().

For example, if you had the HTML ...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

<input type=checkbox name=search value=1>
<input type=checkbox name=search value=2>
<input type=checkbox name=search value=3>
<input type=checkbox name=search value=4>
... then you could use ...
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

use CGI qw(:standard);

my @values = param('search');
my $regex = join( "|" => param('search') );
... to get the values of all the checked boxes into the array @values or turn them into a regex that you can use with /$regex/.

Don't forget to untaint that data! :-)

Also, if you're going to use the DBI module to interface with your database, you can (ab)use its quote function and slap the values safely into a SQL statement.


Jun 13, 2000, 10:14 PM

Post #3 of 4 (895 views)
Re: Is there a better way of doing search [In reply to] Can't Post

Thanks for the help. I tried using the same name for checkboxes. Now i have the array with the checked values. How do i process each one with the database value. I have unique columns for the 4 checkboxes.
for example, i have
@values = (1,3,4). All the checkboxes have colums in the checkbox. How do we match the checkbox values with the database columns
Please help me

User / Moderator

Jun 14, 2000, 11:29 AM

Post #4 of 4 (895 views)
Re: Is there a better way of doing search [In reply to] Can't Post

It's hard to give good examples without knowing exactly what you're trying to do, but the below code would perform a query against any of the wanted columns (assuming the value= property of your checkboxes are set to column names) or use a default list if they didn't select any.
<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

my $default = 'one, three';
my $stmt = sprintf(
'SELECT %s FROM table',
param('search') ?
join( ", " => param('search') )
: $default
$dbh->prepare( $stmt ) | | die $dbh->errstr;</pre><HR></BLOCKQUOTE>
[ Huh ... UBB keeps inserting spaces between the &#0124; &#0124;, so be wary if you cut n' paste ]

[This message has been edited by Kanji (edited 06-14-2000).]


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives