php - Ajax search with two dropdown -
i need have search perform 2 drop down box using ajax. main file is
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#display").hide(); var country =""; var option = ""; $(".selectpicker").on('change', function () { option=$(this).val(); mycall(option,country) }); $(".bfh-selectbox-filter").on('blur', function () { country=$(this).val(); mycall(option,country) }); }); function mycall(option,country){ if (country.length > 0 && option.length > 0) { $.ajax({ type: "post", url: "search.php", data: {country: country, option:option}, success: function (data) { $("#display").html(data).show(); } }); } else { $("#display").html('please select both fields').show(); } }
<div class="col-md-5 col-sm-5"> <select class="form-control selectpicker" required> <option value="">select option</option> <option value="batsman">batsman</option> <option value="bowler">bowler</option> <option value="allrounder">all rounder</option> <option value="wicket keeper">wicket keeper</option> </select>
<script> $(document).ready(function(e) { $(document).find(".bfh-countries input[type=hidden]").attr("name","country"); });
this search.php code. need result display after search button clicked. result have display combinatin of selection these 2 drop downs.
if (isset($_post['country']) && isset($_post['option'])) { $country = trim($_post['country']); $option = trim($_post['option']); $query2 = mysql_query("select * tbl_users inner join tbl_cricketerattr on users_id = tbl_users_users_id country='$keyword' , option='$option'"); $result= "<ul id='playerlist'>"; while ($query3 = mysql_fetch_array($query2)) { $result.= "<li id='list' onclick='fill()'>". $query3['first_name'].":". $query3['country']."</li>"; } $result.= "</ul>"; echo $result;}
please help!!
try way add search button perform search.if don't want can use change event.
your html , script:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <script type="text/javascript"> $(document).ready(function () { $("#display").hide(); var country =""; var option = ""; $(".selectpicker").on('change', function () { option=$(this).val(); mycall(option,country) }); $("ul li a").on('click', function () { country=$(this).data("option"); mycall(option,country) }); }); function mycall(option,country){ console.log(option+":"+country); if (country.length > 0 && option.length > 0) { $("#display").html(""); $.ajax({ type: "post", url: "search.php", data: {country: country, option:option}, success: function (data) { $("#display").html(data).show(); } }); } else { $("#display").html('please select both fields').show(); } } </script> <div class="col-md-5 col-sm-5"> <select class="form-control selectpicker" required> <option value="">select option</option> <option value="batsman">batsman</option> <option value="bowler">bowler</option> <option value="allrounder">all rounder</option> <option value="wicket keeper">wicket keeper</option> </select> </div> <div class="col-md-3 col-sm-3"> <div class="bfh-selectbox bfh-countries" data-country="us" data-flags="true"> <input type="hidden" value=""> <a class="bfh-selectbox-toggle" role="button" data-toggle="bfh-selectbox" href="#"> <span class="bfh-selectbox-option input-medium" data-option=""></span> <b class="caret"></b> </a> <div class="bfh-selectbox-options"> <input type="text" class="bfh-selectbox-filter"> <div role="listbox"> <ul role="option"> </ul> </div> </div> </div> </div> <div id="display"></div>
your search.php: have not tested it,see if has problem feel free ask.
<?php include('includes/db.php'); if (isset($_post['country']) && isset($_post['option'])) { $country = trim($_post['country']); $option = trim($_post['option']); $query2 = mysql_query("select * tbl_users inner join tbl_cricketerattr on users_id = tbl_users_users_id country='$keyword' , option='$option'"); $result= "<ul id='playerlist'>"; while ($query3 = mysql_fetch_array($query2)) { $result.= "<li id='list' onclick='fill()'>". $query3['first_name'].":". $query3['country']."</li>"; } $result.= "</ul>"; echo $result; }
Comments
Post a Comment