javascript - Jquery and PHP , autocomplete -
so found out jquery auto complete , add web-page. want hook php code can search sql database. whenever try run auto complete,it doesnt seem find php array im passing ( im trying array work now) . can help?
jquery code
<!doctype html> <html lang="en"> <head> <meta charset="utf-8"> <title>jquery ui autocomplete - default functionality</title> <link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/smoothness/jquery-ui.css"> <script src="//code.jquery.com/jquery-1.10.2.js"></script> <script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script> <script> $(function() { $( "#tags" ).autocomplete({ source: "test.php" }); }); </script> </head> <body> <div class="ui-widget"> <label for="tags">tags: </label> <input id="tags"> </div> </body> </html>
php code
<?php $data[] = array( 'c++','java','javscript',"c#" ); echo json_encode($data); ?>
this updated version of answer should resolve deprecated sql driver , injection issue. need replace second_columnname
actual column's name. aside think should work.
<?php try { $dbh = new pdo('mysql:host=localhost;dbname=db','username','password'); } catch (pdoexception $e) { print "error!: " . $e->getmessage() . "<br/>"; die(); } if(empty($_request['term'])) exit(); //require_once('connect.php'); connection db in file connection not needed $query = 'select name, second_columnname locations name ? order id asc limit 0,10'; $stmt = $dbh->prepare($query); $stmt->execute(array(ucfirst($_request['term']) . '%')); $data = array(); while ($row = $stmt->fetch(pdo::fetch_assoc)) { $data[] = array( 'label' => $row['name'], 'value' => $row['second_columnname'] ); } echo json_encode($data); flush();
links:
http://php.net/manual/en/pdo.prepared-statements.php
http://php.net/manual/en/pdo.connections.php
https://www.owasp.org/index.php/sql_injection_prevention_cheat_sheet
how can prevent sql injection in php?
also not sure if there else inside connect.php
, might need bring back.
Comments
Post a Comment