php - search the database based on the value selected from database -


i have code cascade drop down list works fine, there 2 drop down list , 2nd 1 depends on first one, wish perform search (from database table )based on parameter choosed second drop down list. have code search also, don't know how combine search code cascade drop down list code

there 2 pages cascade drop down list. first index.php , second fetch_state.php. there code is

index.php

<body>     <div id="container">         <div id="body">             <div class="mhead"><h2>cascaded dropdown jquery ajax , php - infotuts</h2></div>                 <form class="form-horizontal" role="form" action="" enctype="multipart/form-data" method="post">                     <div id="dropdowns">                         <div id="center" class="cascade">                             <?php                                 $sql = "select * search_parent order searchname";                                 $query = mysqli_query($con, $sql);                             ?>                             <label>country:                                 <select name="country" id = "drop1">                                     <option value="">please select</option>                                         <?php while ($rs = mysqli_fetch_array($query, mysqli_assoc )) { ?>                                     <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["searchname"]; ?></option>                                     <?php } ?>                                 </select>                             </label>                         </div>                          <div class="cascade" id="state"></div>                          <div id="city" class="cascade"></div>                      </div>                      <div class="col-md-4 col-sm-6">                         <div class="media-body">                             <div class="col-md-8">                                 <input class="btn btn-primary" value="search" type="submit" name="submit">                             </div>                         </div>                     </div>                 </form>                  </div>     </div>      <script src="jquery-1.11.1.js"></script>     <script>     $(document).ready(function()         {             $("select#drop1").change(function(){                 var parent_id =  $("select#drop1 option:selected").attr('value');                  // alert(parent_id);                     $("#state").html( "" );                 //$("#city").html( "" );                 if (parent_id.length > 0 ){                      $.ajax({                         type: "post",                         url: "fetch_state.php",                         data: "parent_id="+parent_id,                         cache: false,                         beforesend: function (){                                  $('#state').html('<img src="loader.gif" alt="" width="24" height="24">');                             },                         success: function(html){                                     $("#state").html( html );                             }                         });                     }                  });         });     </script> </body> 

fetch_state.php

<?php include("connection.php"); $parent_id = trim(mysql_escape_string($_post["parent_id"]));  $sql = "select * features parent_id = ".$parent_id ." order fname"; $count = mysqli_num_rows( mysqli_query($con, $sql) ); if ($count > 0 ) { $query = mysqli_query($con, $sql); ?> <label>state:  <select name="state" id="drop2">     <option value="">please select</option>     <?php while ($rs = mysqli_fetch_array($query, mysqli_assoc)) { ?>     <option value="<?php echo $rs["id"]; ?>"><?php echo $rs["fname"]; ?></option>     <?php } ?> </select> </label> <?php      } ?> <script src="jquery-1.11.1.js"></script> 

the code above helps in cascade list code search wish perform

<?php $fname = mysqli_real_escape_string($con, $_post['fname']);  $sql1 = "select * office fname '%$fname%'; $result = mysqli_query($con, $sql1);  if (mysqli_num_rows($result) > 0)      {         while($row = mysqli_fetch_assoc($result)) {         echo  $row["fname"];         //would data table here     } } else {     echo "0 results"; } mysqli_close($con); ?> 

as mentioned earlier also, when user selects value second dropdown list wish search result based on parameter selected in 2nd drop down list , result should displayed on same page below search bar

after html code add

<div class="showsearch"></div> 

js code

$(document).ready(function(){     $('#drop2').on('change',function(){          var fname = $(this).val();          // rename file include $fname  get_search_data.php          if(fname !== ""){             $.post('get_search_data.php',{fname: fname},function(data){             $('.showsearch').html(data);             });           }     }); }); 
  • after code should display $row['fname'] showsearch div
  • you have include("connection.php"); in get_search_data.php

get_search_data.php

<?php include("connection.php"); if(isset($_post['fname'])){ $fname = mysqli_real_escape_string($con, $_post['fname']); } $sql1 = 'select * office fname "%'.$fname.'%"'; $result = mysqli_query($con, $sql1);  if (mysqli_num_rows($result) > 0)      {         while($row = mysqli_fetch_assoc($result)) {         echo  $row["fname"]; // data should appear in showsearch div     } } else {     echo "0 results"; } mysqli_close($con); ?> 

Comments

Popular posts from this blog

python - mat is not a numerical tuple : openCV error -

c# - MSAA finds controls UI Automation doesn't -

wordpress - .htaccess: RewriteRule: bad flag delimiters -