php - auto search the keyword in textarea -
this coding. coding allows me search keyword in database , worked fine. how can search keyword without click search button? want same search function in google search. can automatically search , review without hit button.
<html> <head> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1"> <title>search contacts</title> </head> <p><body> <h3>search contacts details</h3> <p>you may search either first or last name</p> <form method="post" action="search.php?go" id="searchform"> <input type="text" name="question"> <input type="submit" name="submit" value="search"> </form> <?php if(isset($_post['submit'])){ if(isset($_get['go'])){ if(preg_match("/^[ a-za-z]+/", $_post['question'])){ $question=$_post['question']; $con = mysqli_connect("localhost","root",""); mysqli_select_db($con,"search")or die(mysqli_error($con));; $sql="select id , question question question '%" . $question . "%'"; //-run query against mysql query function $result=mysqli_query($con, $sql); while ($row=mysqli_fetch_array($result)){ $question =$row['question']; $id=$row['id']; echo "<ul>\n"; echo "<li>" . " " . $question . "</li>\n"; echo "</ul>"; } } else{ echo "<p>please enter search query</p>"; } } } ?> </body> </html> </p>
well, way google bit different. however, can use javascript (jquery easiest here) send xhr, retrieve results. this
$('input[name="question"]').on('keyup', function (e) { $.ajax({ url: "search.php?go", method: 'post', data: { query: e.target.val() } }).done(function(data) { // output data want }); }
of course, use get
http method, that's preference.
now, aside this, looks have sql injection vulnerability in query. essentially, if input 'drop database question
search query, drop database. should doing using prepared statements using mysql::prepare
, see docs here.
final note: google not way. best way achieve blazing fast search results using websockets maintain socket connection server, , letting search run through document database such elasticsearch, optimised full-text search.
Comments
Post a Comment