Skip to content

Instantly share code, notes, and snippets.

@djoudi
Last active December 24, 2020 10:07
Show Gist options
  • Save djoudi/87cffc6e24e530bc757959270f2acacd to your computer and use it in GitHub Desktop.
Save djoudi/87cffc6e24e530bc757959270f2acacd to your computer and use it in GitHub Desktop.
<?php
require_once 'inc/config.php';
$q = (isset($_GET['nom']))?$_GET['nom']:null;
$sql = "SELECT * FROM clients WHERE nom LIKE '%$q%'";
$res = mysqli_query($connect,$sql) or die(mysqli_error($res));
$num = mysqli_num_rows($res);
$client = [];
if ($num>0) {
while ($data = mysqli_fetch_array($res)):
$client[] = $data['nom'];
endwhile;
$clientJson = json_encode($client);
echo $clientJson;
}
?>
$(document).ready(function() {
$(document).on('keyup', 'input[name=search]', function(event) {
event.preventDefault();
var data = $(this).val()
$.ajax({
url:'action.php',
type: 'GET',
data: {nom:data},
beforeSend:function () {
// $("#forms").trigger("reset");
// $('#btn').text('Loading ...').removeClass('btn-primary').addClass('btn-danger')
},
success:function(req){
$("#result").html('')
var reqJson = jQuery.parseJSON(req)
for (var i = 0; i < reqJson.length; i++) {
$("#result").append('<li>'+reqJson[i]+'</li>')
}
console.log(jQuery.parseJSON(req));
}
})
});
});
<?php
include 'inc/header.php';
require_once 'inc/config.php';
?>
<div class="container mt-5" >
<div class="row">
<div class="col-12">
<div class="form-group">
<input type="search" name="search" class="form-control" placeholder="Search">
<ul class="list-unstyled bg-white text-success" id="result">
</ul>
</div>
</div>
<div class="col-12">
</div>
</div>
</div>
<?php
include 'inc/footer.php';
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment