开发者

PHP Search w/ categories

开发者 https://www.devze.com 2023-03-12 10:40 出处:网络
I wanted users to filter searches by categories I have 3 PHP files. One named searchbycity.php, searchbystate.php, and the default search.php

I wanted users to filter searches by categories

I have 3 PHP files. One named searchbycity.php, searchbystate.php, and the default search.php

My question is, how would I set it up so I could click on a radio button and the search bar would know which php file to search for with that info?

Here's how I have the whole radio button thing laid out so far

<input type='text' size='70' name='search'> 
<input type='image' value='search' src='images/tickmark.png'></a><br>
  Search by &nbsp;
<input type="radio" onclick="eng = this.value;" checked name="sengines"
  value="http://www.google.com/search?q=" />
  City
<input type="radio" o开发者_如何学运维nclick="eng = this.value;" name="sengines"
  value="http://www.altavista.com/web/results?q=" />State

(Ignore the Google search and the AltaVista search, I got it from a website:P)


You could write this html code (note the substitution of radio strings values with integers):

<form action="search.php" method="GET">
   <input type='text' size='70' name='search'> 
   <input type='image' value='search' src='images/tickmark.png'></a><br>
     Search by &nbsp;
   <input type="radio" onclick="eng = this.value;" checked name="sengines"
    value="1" />
    City
   <input type="radio" onclick="eng = this.value;" name="sengines"
    value="2" />State
   <input type="submit" />
</form>

When the user push the send button the search.php page will be execute (server side). This page may contains the following code:

<?php
   if(is_integer($_GET['sengines']) && is_string($_GET['search'])){

       switch($_GET['sengines']){

          case 1: include_once "searchbycity.php";
                  searchByCity($_GET['search']);
                  break;

          case 2: include_once "searchbystate.php";
                  searchByState($_GET['search']);
                  break;

       }

   }
?>

So, if the user selected the first radio button the searchbycity.php file and the hypothetical function called searchByCity present in the file will be called passing the $_GET['search'] value submitted by form. Else, will be included the searchbystate.php file and... the logic will be analogue.

Pay attention: the data sended by form must be sanitized by using the filter functions. In the code there is a first level of checking by using is_integer and is_string functions.

0

精彩评论

暂无评论...
验证码 换一张
取 消