I am in the following dilemma, which may be simple for many here, however, as I am a beginner, I am experiencing a major difficulty in a system I am supporting ...
When you click a button, you run a script that returns data in JSON. However, with each click, it is as if it folds the execution of this script (Execute 2x, then runs 4, 8, 16, etc.).
You who have more experience in the area, can you give me a light, please?
I am sending the 2 script functions, in case the two are in some conflict, and the requested PHP file.
selectDB
functionselectDB(){$(document).ready(function(){varpos=document.getElementById('dgn_id').value;varpostURL="busca_dgn_pri.php";
$.ajax({
async: false,
type: "POST",
cache: 'false',
url: postURL,
data: {dgn_id: pos},
success: function(html){
var dgnData = $.parseJSON(html);
$('#dgn_pri')
.append($("<option></option>")
.attr("value",dgnData.dgn[0].dgn_pri)
.attr("selected","selected")
.attr("disabled","disabled")
.text(dgnData.dgn[0].dgn_pri));
}
});
});
}
addOptionSelect
function addOptionSelect(){
$(document).ready(function () {
$("option").remove();
$('#btnprox').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btnante').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btnprim').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btnulti').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btnnovo').click(function(){
$("option").remove();
addOptionSelect();
});
$('#btnexcl').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btncanc').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
$('#btnsalv').click(function(){
$("option").remove();
addOptionSelect();
selectDB();
});
var sel = $('input[type=checkbox]:checked').map(function(_, campo) {
var nomeCampo = $(campo).attr('name');
return nomeCampo;
}).get();
$.each(sel, function(key, value){
if (($('option[value='+key+']').length == 0) && (value != "sem_diag")) {
var campoTxt = $('label[for='+value+']').html();
$('#dgn_pri')
.append($("<option></option>")
.attr("value",campoTxt)
.text(campoTxt));
} else {
$("option").remove();
}
});
});
}
PHP file
$pos = $_REQUEST['dgn_id'];
$sql = "SELECT dgn_id, dgn_pri FROM nut_diagnutric WHERE dgn_id = $pos";
$result = $DB->query($sql);
$rows = array();
while($row = $DB->fetchArray($result)){
$rows[] = $row;
}
$qryResult = array();
$qryResult['dgn'] = array_unique($rows);
echo json_encode($qryResult);