I am trying to display an unlisted search results which is able to have a click event to a target html page based on the search from the database (mysql).
I am using PhoneGap.
So far I managed to implement a simple PHP and jQuery script that gives me a simple pop down from an auto-complete search field which completes the search. But I want the user to be able to click on it and be directed to a different page with unique information displayed from the database.
PHP:
<?php
header("Access-Control-Allow-Origin: *");
$con = mysqli_connect('localhost', 'root', '', 'qc_artwork') or die ("could not connect database");
//get search term
$searchTerm = $_GET['term'];
//get matched data from name table
$query = $con->query("SELECT * FROM artwork WHERE name LIKE '%".$searchTerm."%' ORDER BY name ASC");
while ($row = $query->fetch_assoc()) {
$data[] = $row['name'];
}
//return json data
echo json_encode($data);
?>
HTML:
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/ionic.css">
<link rel="stylesheet" href="css/jquery-ui.css">
<script type="text/javascript" src="cordova.js"></script>
<script type="text/javascript" src="js/jquery.js"></script>
<script src="js/jquery-1.10.2.js"></script>
<script src="js/jquery-ui.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(function() {
$( "#searchField" ).autocomplete({
source: 'http://localhost/search.php'
});
});
});
</script>
</head>
<body>
<div class="bar bar-header bar-positive" style="margin-bottom:80px;">
<a href="index.html" class="button button-clear">Home</a>
<h1 class="title">Search Database (JSON)</h1>
</div><br/><br/>
<p>
<input type="text" id="searchField" placeholder="AF artwork">
</p>
</body>
</html>
My SQL table consists of 5 columns:
- id
- name
- path
- barcode
- type
The end-goal plan is to eventually display those unique images/pdfs into the unique pages from the selected search.
But my main question is How am I able to let the user to be able to click on an unlisted result and be directed to a different page with unique information displayed from the database?
Use the on
select
callback (its in the docs), include theid
,label
andvalue
in your json then on click use awindow.location.href
to navigate to the page. You could also load the content in using an ajax call.So for example using ajax get the content:
And your
search.php
file, would look like the following which is safe from SQL injection as it uses prepared queries:Untested but should work.