发出从getJSON数据传递变量到php

时间:2022-03-26 10:02:49

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:

我有一个jQuery getJSON,我想将一些数据传递给php。奇怪的是我无法传递var。在我的代码中,我有另一个区域,这个工作正常,如果我硬编码字符串也可以正常工作,但如果我把一个有效的var也工作正常,是不行的。这是我的代码:

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并且工作正常的部分:

$.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!

所以,我真的不知道发生了什么。该变量甚至可以在getJSON下工作,但不能在该特定的getJSON方法下工作。有人能帮我吗?谢谢!

1 个解决方案

#1


1  

<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",

好吧,我发现getJSON响应归因于div要求的“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

它根本没有回应

#1


1  

<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",

好吧,我发现getJSON响应归因于div要求的“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

它根本没有回应