sammy
sammy

Reputation: 717

filter a search with radio button PHP SQL

i have a search list that works property. theh search works according to finding if searched word is like name, family and job. now i want too seprate this searching. i mean, creat three radio button. when cleck on first button search by name, second one, search by family, and third one search by job.

so my base code is here:

here need to add three radio button. (searchform.php)

<form name="form1"  dir="rtl" method="post" action="searchresults.php">
<label for="search"> search </label>
<input name="search" type="text" size="40" maxlength="50" placeholder="you can search">
<input type="submit" name="submit" value="search"/> <br/>
</form>

and search cod is here:

searchresult.php

<?php

   $db_host = 'localhost';
   $db_name= 'site';
   $db_table= 'tablesite';
   $db_user = 'root';
   $db_pass = '';


   $con = mysql_connect($db_host,$db_user,$db_pass) or die("خطا در اتصال به پايگاه داده");
$selected=mysql_select_db($db_name, $con) or die("خطا در انتخاب پايگاه داده");
mysql_query("SET CHARACTER SET  utf8");
$dbresult=mysql_query("SELECT tablesite.name,
                              tablesite.family,
                              job_list.job_name,
                       FROM  $db_table
                       INNER JOIN relation
                       on tablesite.id_user=relation.user_id
                       INNER JOIN job_list
                       on relation.job_id=job_list.job_id 
                       WHERE job_name LIKE '%".$_POST['search']."%' OR
 name LIKE '%".$_POST['search']."%' OR
 family LIKE '%".$_POST['search']."%'",$con);
?>
   <title>نتایج جستجو</title><fieldset class="fdex"  dir="rtl">
    <legend><span class="style4">نتایج جستجوی مشاغل</span></legend>

   <?php


   while ($row = mysql_fetch_array($dbresult, MYSQL_ASSOC)) {

   printf("نام: %s   %s &nbsp&nbsp&nbsp شغل: %s &nbsp&nbsp&nbsp شماره تلفن: %s <br>", $row["name"], $row["family"], $row["job_name"], $row["phone_number"]);

}
?>

as you see, in this search method, when user write in search box, example school, it find all results by school that may be family of a person. soo i need to seprate this search to three radio buttion. first name, second family, third job.

Upvotes: 3

Views: 3911

Answers (1)

Tristan
Tristan

Reputation: 3321

Just add the radio buttons to the form.

<form name="form1"  dir="rtl" method="post" action="searchresults.php">
<label for="search"> search </label>
<input name="search" type="text" size="40" maxlength="50" placeholder="you can search">
<input type="radio" name="search_type" value="job_name" checked="checked">Job<br>
<input type="radio" name="search_type" value="family">Family<br>
<input type="radio" name="search_type" value="name">Name
<input type="submit" name="submit" value="search"/> <br/>
</form>

And use the post field and value in your sql query. mysql is depreciated so you should use mysqli and it is essential that you escape inputs to prevent injection attacks.

$field = mysql_real_escape_string($_POST['search_type']);
$value = mysql_real_escape_string($_POST['search']);
$sql = "SELECT tablesite.name,
          tablesite.family,
          job_list.job_name,
   FROM  $db_table
   INNER JOIN relation
   on tablesite.id_user=relation.user_id
   INNER JOIN job_list
   on relation.job_id=job_list.job_id 
   WHERE 
   $field LIKE '%".$value."%'";
 $dbresult=mysql_query($sql,$con);

You may also like to validate a search value before running the query and check for a result before looping through each row.

Upvotes: 3

Related Questions