I have this input field that reads the user' input in "index.php"
<input type="text" name="medname" id="medname" class="search-input" placeholder="Search..." name="">
<div class="result"></div>
Then this AJAX live search function receives the input, sends it to "livesearch.php" file to search the database for matching data. Then receives a paragraphe element to display it as a search result.
AJAX function in "index.php"
<script>
$(document).ready(function() {
$('.search input[type="text"]').on("keyup input", function() {
/* Get input value on change */
var inputVal = $(this).val();
var resultDropdown = $(this).siblings(".result");
if (inputVal.length) {
$.get("livesearch.php", {
term: inputVal
}).done(function(data) {
// Display the returned data in browser
resultDropdown.html(data);
});
} else {
resultDropdown.empty();
}
});
// Set search input value on click of result item
$(document).on("click", ".result p", function() {
$(this).parents(".search").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
livesearch.php
if(isset($_REQUEST["term"])){
// Prepare a select statement
$sql = "SELECT * FROM medicine WHERE mName LIKE ?";
if($stmt = mysqli_prepare($link, $sql)){
// Bind variables to the prepared statement as parameters
mysqli_stmt_bind_param($stmt, "s", $param_term);
// Set parameters
$param_term = $_REQUEST["term"] . '%';
// Attempt to execute the prepared statement
if(mysqli_stmt_execute($stmt)){
$result = mysqli_stmt_get_result($stmt);
// Check number of rows in the result set
if(mysqli_num_rows($result) > 0){
// Fetch result rows as an associative array
while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
echo "<p>" . $row["mName"] . "</p>";
}
} else{
echo "<p>No matches found</p>";
}
} else{
echo "ERROR: Could not able to execute $sql. " . mysqli_error($link);
}
}
Now for my problem a have another page called "map.php". I need to send "mID : Id of a medicine" and "mName : Name of a medicine" that the user's selected to that page.
How can i do that?
I solved the issue by using json. I don't understand it very much but it works(CSS not included).
index.php
index.php (Input and results field)
search.php