I'm trying to use jquery-ui native escapeRegex with my search term which I'm failing to do.
The code blow is to get some customer names from the database then list them accordingly. I've one search input which search the main customer then I use the second one to search the secondary customer which get sorted according to the first selected main customers ID, this ID is stored in the text input field named cId.
When the user goes to select the secondary customer my second autocomplete will take the main customer ID form the cId field then send it to PHP which is listed below and get the second customer name and the ID. Which is sent back via JSON as an array.
On my first autocomplete I got the mentioned RegEx function to list out customer names according to the first letter I typed. For an example if I type letter A the script will list all the names starting with A. This is what I want do on my second autocomplete. But I can't understand how to apply the escapeRegex function. I tried out some old answers but none of them worked with my code.
Main PHP page code,
<div class="usrCreate-form-right">/* Search 1*/
<label for="cuName">Customer / Kunde:</label><br>
<input id="cuName" name="cuName" class="usrCreate-form-inputs">
<input id="cId" class="usrCreate-form-inputs" hidden><br />
<script type="text/javascript" language="JavaScript">
var kNames = [
<?php
//Loads the data from kunde data base to the MultiDimentional Array
while ($getKundeRow = $getKundeQuery -> fetch(PDO::FETCH_ASSOC)){ echo '{label:"'.$getKundeRow['customer_name'].'", value:"'.$getKundeRow['kId'].'"},'; }
?>
];
$('#cuName').autocomplete({
source: function( request, response ) {
var matcher = new RegExp( "^" + $.ui.autocomplete.escapeRegex( request.term ), "i" );
response( $.grep( kNames, function( item ){
return matcher.test( item.label );
}) );
},
minLength: 1,
focus: function (event, ui) {
$('#cuName').val(ui.item.label);
return false;
},
select: function (event, ui) {
$('#cuName').val(ui.item.label);
$('#cId').val(ui.item.value);
return false;
}
});
</script>
</div>
<div class="usrCreate-form-left">/*Search 2*/
<label for="tName">Target / Mal:</label><br>
<input id="tName" name="tName" class="usrCreate-form-inputs">
<input id="mid" name="mid" class="usrCreate-form-inputs"><br>
<script type="text/javascript" language="JavaScript">
$('#tName').autocomplete({
source: function (request, response) {
$.ajax({
url: "addNewJobProcess.php",
dataType: "json",
data:{
kId: $('#cId').val()
},
success:function (data) {
var re = $.ui.autocomplete.escapeRegex(request.term),
matcher = new RegExp("^" + re, "i");
response($.map(data.myMalData, function (item) {
return{
label: item.mal_name,
value: item.mId
}
}))
}
})
},
minLength: 1,
focus:function (event, ui) {
$('#tName').val(ui.item.label);
return false;
},
select: function (event, ui) {
$('#tName').val(ui.item.label);
$('#mid').val(ui.item.value);
return false;
}
})
</script>
</div>
PHP code for the data base,
include_once("../deLink/deLinker.php");
if (isset($_GET['kId'])){
$malNames = array();
$getMalQuery = $dbConnect -> prepare("SELECT * FROM mal_list WHERE kId = :kid");
$getMalQuery -> bindValue(':kid', $_GET['kId']);
$getMalQuery -> execute();
while($getMalRow = $getMalQuery -> fetch(PDO::FETCH_ASSOC)){
$malNames['myMalData'][] = array(
'mal_name' => $getMalRow['Mal Name'],
'mId' => $getMalRow['mId']
);
}
echo json_encode($malNames);
}