I have a select that is dynamically filled when entering a store and a date range, I configure it so that that select is completed with the employees once that same select is pressed with the click event. The problem arises that when I load the list of employees and select any one, the ajax request is executed again and it automatically changes me to the first one in the list. An example would be, when I click on the select it shows me a, b and c, when I click on c it selects a. I have read on some page about using the one event but the problem is that if I change the store I still see the employees of the store that was selected first, I also tried with the change event and directly it is not filled with the employees, I tried with e.preventDefault, e.stopImmediatePropagation and e.stopPropagation (), and it doesn't work. I share the code to see if you can help me. Thanks a lot.
$(document).ready(function(){
$("#user").on("click", function(e) {
var dFec = $('#dFec').val();
var hFec = $('#hFec').val();
var store= $('#store').val();
fillUser(store,dFec,hFec);
});
});
function fillUser(store,dFec,hFec){
var request = $.ajax({
url: "getUser.php",
method: "POST",
data: {store: store, dFec:dFec, hFec:hFec},
dataType: "html",
error: function(){
alert("error");
},
});
request.done(function( msg ) {
$( "#user" ).html(msg);
});
}
HTML
<h5 class="h3 font-weight-normal text-white text-center bg-info" style="width:35%; margin:auto;">Store</h5>
<div class="form-group text-center">
<select name="store" id="store" class="form-control form-control-sm" style="width:35%; margin:auto;">
<option value="0">Select Store</option>
<?php while($row = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC)) { ?>
<option value="<?php echo $row['id_store']; ?>"><?= $row['nameStore']; ?></option>
<?php } ?>
</select>
</div>
<h5 class="h3 font-weight-normal text-white text-center bg-info" style="width:35%; margin:auto;">Fecha</h5>
<table class="table table-sm text-center" style="margin:auto; width:35%">
<tr>
<th>From</th>
<th>To</th>
</tr>
<tr>
<div id="picker"></div>
<td><input name="dFec" id="dFec" readonly="readonly" style="text-align:center"></td>
<div id="picker1"></div>
<td><input name="hFec" id="hFec" readonly="readonly" style="text-align:center"></td>
</tr>
<div id="other">
</div>
</table>
<h5 class="h3 font-weight-normal text-white text-center bg-info" style="width:35%; margin:auto;">user</h5>
<div class="form-group text-center">
<select name="user" id="user" class="form-control form-control-sm" style="width:35%; margin:auto;"></select>
</div>
getUsuario.php
//query and stmt are at beggining of the code
$store = $_POST['store'];
$dFec = date("Ymd", strtotime($_POST['dFec']));
$hFec = date("Ymd", strtotime($_POST['hFec']));
while ($rowB = sqlsrv_fetch_array( $stmt, SQLSRV_FETCH_ASSOC))
{
$html.= "<option value='".$rowB['user']."'>".$rowB['user']."</option>";
}
echo $html;