javascript - Working with search suggestion -
i have following search suggestion script, user types dynamically result updated. want when user click on 1 of suggestion takes them following:searchpage.php?user_query=what user has typed in input
below script:
$(function () { $(".search").keyup(function () { var searchid = $(this).val(); var datastring = 'search=' + searchid; if (searchid != '') { $.ajax({ type: "post", url: "search.php", data: datastring, cache: false, success: function (html) { $("#result").html(html).show(); } }); } return false; }); jquery("#result").live("click", function (e) { var $clicked = $(e.target); var $name = $clicked.find('.name').html(); var decoded = $("<div/>").html($name).text(); $('#searchid').val(decoded); }); jquery(document).live("click", function (e) { var $clicked = $(e.target); if (!$clicked.hasclass("search")) { window.open('searchpage.php?user_query=', '_self', false); } }); $('#searchid').click(function () { jquery("#result").fadein(); }); });
at current state open window searchpage.php doesn't take account in url user has entered. desired result searchpage.php?user_query=what ever want
update:
<form method="get" action="searchpage.php" enctype="multipart/form-data" autocomplete="off"> <input type="text" class="search" id="searchid" name="user_query" id="searchbar" placeholder="search courses"/> <input type="submit" id="searchbutton" name="search" value="search" class="btn btn-danger" autocomplete="off"/> <div id="result"></div> </form>
you can value of search string this:
document.getelementbyid('searchid').value
so, appending query this:
window.open('searchpage.php?user_query=' + document.getelementbyid('searchid').value,'_self',false);
depending on in search box, might have issues address, such search term includes &
symbol (e.g. ?user_query=search&find example come across search on server) - don't want accidentally pass param intend part of search string. address might convert character:
var searchstring = document.getelementbyid('searchid').value.replace(/&/g,'&') window.open('searchpage.php?user_query=' + searchstring,'_self',false);
Comments
Post a Comment