4
I am in the following dilemma, which may be simple for many here, but as a beginner, I am going through a great difficulty in a system that I am supporting...
By clicking a button, a script which returns data in JSON. However, with each click, it is as if it doubles the execution of this script (Runs 2x, then runs 4, 8, 16, etc).
You who have more experience in the field, can give me a light, please?
I’m sending the 2 functions of the script, in case the 2 are in some conflict, and the PHP file requested.
selectDB
function selectDB(){
$(document).ready(function(){
var pos = document.getElementById('dgn_id').value;
var postURL= "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);
Put the parameter
event
(the function should befunction(event)
) in each function of events also put the following codeevent.preventDefault();
andevent.stopPropagation();
at the beginning of each click event that is called the functionselectDB()
. See if it solves.– KaduAmaral
Put pf more information than JSON has and what you want to do with the data coming from PHP so we can help more.
– Sergio
This function Function selectDB(){ .. } does not need a subfunction : $(Document). ready(Function(){}); because this is only used to load when reading the document, you can pass all the contents of that ready Ocument within its direct method... otherwise it will be reading several times this post.
– Ivan Ferrer