-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathajax.php
37 lines (30 loc) · 1.06 KB
/
ajax.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<?php
// Including Database configuration file.
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "shop_db";
$con = mysqli_connect($servername, $username, $password, $dbname);
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
// Getting value of "search" variable from "script.js".
if (isset($_POST['search'])) {
// Search box value assigning to $Name variable.
$name = $_POST['search']; //search_item
// Search query.
$query = "SELECT name FROM products WHERE name LIKE '$name%' LIMIT 5"; //search_item
// Query execution
$execQuery = mysqli_query($con, $query);
// Creating unordered list to display result.
echo '<ul>';
// Fetching result from database.
while ($result = mysqli_fetch_array($execQuery)) {
// Creating unordered list items.
// Calling javascript function named as "fill" found in "script.js" file.
// By passing fetched result as parameter.
echo "<li onclick='fill(\"{$result['name']}\")'><a>{$result['name']}</a></li>";//search_item
}
echo '</ul>';
}
?>