Jump to content

advanced search form


wkilc

Recommended Posts

Can someone help me code kind of a "advanced search" form, that will get a URL:

 

<form method="get">
<input type="checkbox" name="level" value="PreK" />
<input type="checkbox" name="level" value="Elem" />
<input type="checkbox" name="level" value="MS" />
<input type="checkbox" name="level" value="HS" />
<input type="checkbox" name="subject" value="Math" />
<input type="checkbox" name="subject" value="Reading" />
<input type="checkbox" name="level" value="Science" />
<input type="submit" value="submit" />
</form>

 

So... if I ticked PreK, Elem and Math, the resulting link would be:

www.mysite.com?level=PreK&Elem&subject=Math

 

The most complicated thing... how would I get place the "&" in between variables?

 

Thank you once again.

 

~Wayne

 

 

 

Link to comment
https://forums.phpfreaks.com/topic/211015-advanced-search-form/
Share on other sites

No the URL would be

?level=PreK&level=Elem&level=MS

which will overwrite each other!

 

you need to use an array

ie level[]

 

eg

<?php
echo "<u>level</u><BR />";
if(!empty($_GET['level']))
  foreach($_GET['level'] as $lev){
    echo "$lev <BR />\n";
  }
echo "<u>subject</u><BR />";
if(!empty($_GET['subject']))
  foreach($_GET['subject'] as $sub){
    echo "$sub <BR />\n";
  }
?>
<form method="get">
<input type="checkbox" name="level[]" value="PreK" />
<input type="checkbox" name="level[]" value="Elem" />
<input type="checkbox" name="level[]" value="MS" />
<input type="checkbox" name="level[]" value="HS" />
<input type="checkbox" name="subject[]" value="Math" />
<input type="checkbox" name="subject[]" value="Reading" />
<input type="checkbox" name="level[]" value="Science" />
<input type="submit" value="submit" />
</form>

Thank you!

 

But now the URL contains []

 

?level=PreK[]&level=Elem[]&level=MS[],

 

...which prevents the link from working.

 

Is there a way to remove that but still allow it to echo the variables filtered?

 

I'm very new to this... do I need to define the array here as well?

 

Thanks again.

 

~Wayne

I've modified the query to include:

 

   and ((user_info.PreK LIKE '%$lev%')
     or (user_info.Elem LIKE '%$lev%'))

 

and the form looks like thus:

 

echo "<u>level</u><BR />";
if(!empty($_GET['lev']))
  foreach($_GET['lev'] as $lev){
    echo "$lev <BR />\n";
  }
echo "<u>subject</u><BR />";
if(!empty($_GET['sub']))
  foreach($_GET['sub'] as $sub){
    echo "$sub <BR />\n";
  }
?>
<form action="search_advanced.php" method="get">
<input type="checkbox" name="lev[]" value="PreK" />PreK
<input type="checkbox" name="lev[]" value="Elem" />Elem
<input type="checkbox" name="lev[]" value="MS" />MS
<input type="checkbox" name="lev[]" value="HS" />HS
<input type="checkbox" name="sub[]" value="Math" />Math
<input type="checkbox" name="sub[]" value="Reading" />Reading
<input type="submit" value="submit" />
</form>

 

But... was you said, it's overwriting:

advanced.php?lev[]=PreK&lev[]=Elem

 

...the above URL only gives me Elem, no PreK.

 

~Wayne

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.