Reputation:
I have a jQuery getJSON that I want to pass some data to php. The weird thing is that I can't pass a var. In my code I have another area where this works ok, also if I hardcode the string also works ok, but if I put a valid var that is also working ok, is not working. Here is my code:
This is where I get the string from a dropdown:
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
Here is another piece where I use the same getjson and is working ok:
$.getJSON(url+preco+php, {serv:idserv},null).then(function(data)
{console.log(data);
But this is not working:
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:func}).then(function(data)
If I put:
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:"String"}).then(function(data)
works ok.
So, I really don't know what is going on. The variable works ok even below the getJSON, but is not working at that particular getJSON method. Can someone help me? Thank you!
Upvotes: 1
Views: 125
Reputation:
<script>
var func = "";
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
var showServAdicionais = 1;
var url = "";
var php = ".php"
var preco = "preco";
$(".adi1").click(function(){
$(".serv2").show();
showServAdicionais = 2;
});
$(".add2").click(function(){
$(".serv3").show();
showServAdicionais = 3;
});
$ (".add3").click(function(){
$(".serv4").show();
showServAdicionais = 4;
});
$(".add4").click(function(){
$(".serv5").show();
showServAdicionais = 5;
});
$(function(){
var items="";
$.getJSON("http://ib.esy.es/gestao/_php/servicos_categorias.php",function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.ID+"'>"+item.categoria+"</option>";
});
$("#categoria").html(items);
});
});
var url = "";
function selectCat(){
$('#servicos').change(function() {
$('.serv'+showServAdicionais).val($(this).find(":selected").text());
});
//for textbox use $('#txtEntry2').val($(this).find(":selected").text());
var e = document.getElementById("categoria");
var servSelected = e.options[e.selectedIndex].value;
var items="";
if(servSelected === "1"){
url = "http://ib.esy.es/gestao/_php/servicos_threading";
}
if(servSelected === "2"){
url = "http://ib.esy.es/gestao/_php/servicos_sobrancelhas";
}
if(servSelected === "3"){
url = "http://ib.esy.es/gestao/_php/servicos_manicure";
}
$.getJSON(url+php,function(data){
$.each(data,function(index,item)
{
items+="<option value='"+item.ID+"'>"+item.servico+"</option>";
});
$("#servicos").html(items);
});
};
function selectServ(){
var idserv = $("#servicos option:selected").val();
$.getJSON(url+preco+php, {serv:idserv},null).then(function(data)
{console.log(data);
$(".valor"+showServAdicionais).val(data.preco);
$(".total"+showServAdicionais).val(data.preco);
});
}
$('.toCalculate').keyup( getDiff);
function getDiff(){
var num1=1*$('.valor'+showServAdicionais).val() || 0;
var num2=1*($('.desconto'+showServAdicionais).val())/100 || 0;
var num = (Math.abs(num1 * num2 -num1))
$('.total'+showServAdicionais).val(num.toFixed(2))
}
function showCom(){
var pass = document.getElementById('comissoes').value;
var namefuncionaria = $('.funcionariaTitle').text();
if(func == "Iara" && pass == "teste"){
$('.showComissoes').show()
} else if(func == "Katiuska" && pass == "teste1"){
$('.showComissoes').show()
} else if(func == "Lorena" && pass == "teste2"){
$('.showComissoes').show()
} else if(func == "Adryely" && pass == "teste3"){
$('.showComissoes').show()
} else{
alert("Senha errada");
}
}
function cleanInput(){
$('#comissoes').val("");
}
function hideCom(){
$('.showComissoes').hide();
}
$.getJSON("http://ib.esy.es/gestao/_php/select_com_mes.php", {func:func}).then(function(data)
{console.log(data);
var tr = data
for (var i = 0; i < data.report.length; i++) {
var tr = $('<tr/>');
// Indexing into data.report for each td element
$(tr).append("<td>" + data.report[i].Mes + "</td>");
$(tr).append("<td>" + data.report[i].Total + "</td>");
$('.tableMes').append(tr);
}
});
function teste(){
alert(func)
}
</script>
UPDATE:
Well, I found out that getJSON response is attributed to the div requested "Lorena",
<div class="funcionariaTitle">Lorena</div>
is the response that I have, even if I change here
var func = "";
$('.funcionarias').change(function(){
func = $('.funcionarias').val();
$('.funcionariaTitle').text(func);
});
its text. If I request the dropdown value like:
var funcionaria = $( ".funcionarias option:selected" ).text();
it gives no response at all
Upvotes: 1