So I have recently created this small script that searches my Customer Database when the user start typing in a certain box.
This allows them to tie a record to another record to make it easier in the future.
However when the customer they're looking for appears they have to click the name, once they've clicked the name it'll populate the box.
However I need to be able to identify which customer they've clicked on so I can store the customer ID in the database for future.
So far the script i've got will display 3 columns, the customers firstName
, lastName
and ID
. How can i separate the ID away from the firstName
and lastName
to store it in a database?
This is my PHP search code...
try {
$customerConn = new PDO('mysql:host=DATABASE HOST NAME HERE;dbname=DATABASE NAME HERE;charset=utf8', 'USERNAME', 'PASSWORD');
// echo 'client version: ', $conn->getAttribute(PDO::ATTR_CLIENT_VERSION), "\n";
// echo 'server version: ', $conn->getAttribute(PDO::ATTR_SERVER_VERSION), "\n";
$customerConn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$customerConn->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
}
catch(PDOException $err) {
var_dump($err->getMessage());
die('...');
}
// Attempt search query execution
try{
if(isset($_REQUEST["term"])){
// create prepared statement
$sql = "SELECT * FROM customerLeads WHERE lastName LIKE :term";
$stmt = $customerConn->prepare($sql);
$term = $_REQUEST["term"] . '%';
// bind parameters to statement
$stmt->bindParam(":term", $term);
// execute the prepared statement
$stmt->execute();
if($stmt->rowCount() > 0){
while($row = $stmt->fetch()){
echo "<p>".$row['firstName']." ".$row["lastName"]." - ".$row['customerID']."</p>";
}
} else{
echo "<p>No matches found</p>";
}
}
} catch(PDOException $e){
die("ERROR: Could not able to execute $sql. " . $e->getMessage());
}
// Close statement
unset($stmt);
// Close connection
unset($customerConn);
Also I've replaced the database details just for security!
Edit Here is the JavaScript that I have in page
<script src="https://code.jquery.com/jquery-1.12.4.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('.search-box 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("../../scripts/customerSearch.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-box").find('input[type="text"]').val($(this).text());
$(this).parent(".result").empty();
});
});
</script>
and here is the relevant HTML
<div class="form-group search-box">
<label>Customer Name</label>
<input type="text" placeholder="Search Customer" autocomplete="off" name="customerName">
<div class="result"></div>
</div> <!-- end .form-group -->