简体   繁体   中英

jquery ajax call to db no response

I don't understand where the problem is :S

The button call a function with the parameter "sex" and fires the ajax call to ajax.php where I do the mysql query to catch the results and put them in various input box.

When analyze the post with firebug, the response is empty and I got the error.

I picked up the post and tried it directly on page:

ajax.php?pesokg=76&polsocm=&collocm=42&avambracciocm=&anchecm=&altezzacm=176&vitacm=&addomecm=86&moltiplicatore=1.9

and the result is:

{"percentualeMG":"15.7","percentualeMM":"64.1","proteineDay":"121.8","blocchiZona":"17.4","blocchiMin":"0","queryStatus":"Success"} 

that seems correct

ajax.php:

if(!empty($_GET['pesokg']) && !empty($_GET['altezzacm']) && !empty($_GET['collocm']) && !empty($_GET['moltiplicatore'])){

    $cod_user = fix_special_char($_COOKIE['id']);
    $pesokg=fix_special_char($_GET['pesokg']);
    $altezzacm=fix_special_char($_GET['altezzacm']);
    $collocm=fix_special_char($_GET['collocm']);
    $moltiplicatore=fix_special_char($_GET['moltiplicatore']);
    $blocchimin = 0;

    if(!empty($_GET['addomecm'])){ //uomo

        $addomecm=fix_special_char($_GET['addomecm']);
        //calcolo 
        $percentuale1 = (76.5 * log10($addomecm - $collocm)) - (68.7 * log10($altezzacm)) + 46.9; //alert(percentuale1);
        $percentuale2 = (86.01 * log10(($addomecm/2.54 - $collocm/2.54))) - (70.041 * log10($altezzacm/2.54)) + 36.76; //alert(percentuale2);

        $queryField=",addome";
        $queryValue=",".$addomecm;

    }   

    if(!empty($_GET['vitacm']) && !empty($_GET['anchecm']) && !empty($_GET['polsocm']) && !empty($_GET['avambracciocm'])){ //donna


        $vitacm=fix_special_char($_GET['vitacm']);
        $anchecm=fix_special_char($_GET['anchecm']);
        $polsocm=fix_special_char($_GET['polsocm']);
        $avambracciocm=fix_special_char($_GET['avambracciocm']);
        //calcolo 
        $percentuale1 = (105.3 * log10($pesokg)) - (0.2 * $polsocm) - (0.533 * $collocm) - (1.574 * $avambracciocm) + (0.173 * $anchecm) - (0.515 * $altezzacm) - 35.6; 
        //alert(percentuale1);
        $percentuale2 = (163.205 * log10((($vitacm/2.54 + $anchecm/2.54) - $collocm/2.54))) - (97.684 * log10($altezzacm/2.54)) - 78.387; //alert(percentuale2);

        $queryField=",vita,anche,polso,avambraccio";
        $queryValue=",".$vitacm.",".$anchecm.",".$polsocm.",".$avambracciocm;

    }

    $percmedia = round((($percentuale1 + $percentuale2*2) / 3),1);
    $data["percentualeMG"]= "'".$percmedia."'";
    $massamagra = round(($pesokg - $pesokg/100*$percmedia),1);
    $data["percentualeMM"]= "'".$massamagra."'";
    $proteine = $moltiplicatore * $massamagra;
    $data["proteineDay"]= "'".round($proteine,1)."'";
    $blocchi = round(($proteine / 7),1);

    if($blocchi<11){
        $blocchi=11;
        $blocchimin=1;          
    }

    $data["blocchiZona"] = "'".$blocchi."'";
    $data["blocchiMin"] = "'".$blocchimin."'";


    $queryAddProtNeed = "INSERT INTO ".$DBPrefix."prot_need(lastCheck,peso, altezza, collo, moltiplicatore, cod_user, blocchi".$queryField.") VALUES ('".date('Y-m-d')."','".$pesokg."','".$altezzacm."' , '".$collocm."' , '".$moltiplicatore."' , '".$cod_user."' , '".$blocchi."'".$queryValue.")";

    $resultAddProtNeed = mysql_query($queryAddProtNeed,CONN);
    //echo $queryAddProtNeed;

    if($resultAddProtNeed){
        $data['queryStatus'] = 'Success';  
        console.log($data);
        echo json_encode($data);
    }else {
        $data['queryStatus'] = 'Error:'.mysql_error(CONN);  
    }          
}

ajax.js:

function addProtNeed(sex){

    var peso        = $('#peso').val(),
        collo       = $('#collo').val(),
        altezza     =$('#altezza').val(),
        activity    =$('#attivita').val();

if(sex=='uomo'){
    var addome  =$('#addome').val(),
        polso       = "",
        avambraccio ="",
        anche       ="",
        vita        ="";
}
if(sex=='donna'){
    var polso       = $('#polso').val(),
        avambraccio =$('#avambraccio').val(),
        anche       =$('#anche').val(),
        vita        =$('#vita').val(),
        addome      ="";
}
//alert("peso"+peso+"collo:"+collo+"altezza:"+altezza+"activity:"+activity+"addome:"+addome);   
//var noty=myNoty("information","Caricamento del pasto in corso","",false);
     $.ajax({
          type: "POST",
          url: "pages/ajax.php",
          data: { pesokg: peso, polsocm: polso, collocm: collo, avambracciocm: avambraccio, anchecm: anche, altezzacm: altezza, vitacm: vita, addomecm: addome, moltiplicatore: activity},

          dataType: 'json',
          success: function( data ) {
         // alert('ciao');
                  var json              = $.parseJSON(data),
                      percentualeMG     = json['percentualeMG'],
                      percentualeMM     = json['percentualeMM'],
                      proteineDay       = json['proteineDay'],
                      blocchiZona       = json['blocchiZona'],
                      blocchimin        = json['blocchimin'],
                      queryStatus       = json['queryStatus'];                  
                $("#percentualeMG").val(percentualeMG);
                $("#percentualeMM").val(percentualeMM);
                        $("#proteineDay").val(proteineDay);
                        $("#blocchiZona").val(blocchiZona);

                            //alert(json.queryStatus);
                      console.log('addProtNeed success');
            },
            complete:function(){
                //$noty.close();
                //myNoty("information","Caricamento completato");
                    //myNoty("success","Pasto caricato","300","");
                console.log('Calcolo complete');
            },
            error: function(){
                  console.log('error');
                }
        });

}

button that fire the call:

    <button type="button" id="calcola" class="ui-button ui-widget ui-state-default ui-corner-all ui-button-text-only" role="button" aria-disabled="false" onclick="addProtNeed(getCookie('sesso'))">                                <span class="ui-button-text">                                               Calcola                                         </span>                                         </button>

Error:

parsererror
SyntaxError: JSON.parse: unexpected end of data at line 1 column 1 of the JSON data

return window.JSON.parse( data );

Any idea?

Thx

Maybe is the type of ajax. You have POSt in your ajax and in the PHP you get the parameters with $_GET.
Try change the ajax parameter type

$.ajax({
          type: "GET",
          url: "pages/ajax.php",
          data: { pesokg: peso, polsocm: polso, collocm: collo, avambracciocm: avambraccio, anchecm: anche, altezzacm: altezza, vitacm: vita, addomecm: addome, moltiplicatore: activity},

          dataType: 'json',
          success: function( data ) {
         // alert('ciao');
                  var json              = $.parseJSON(data),
                      percentualeMG     = json['percentualeMG'],
                      percentualeMM     = json['percentualeMM'],
                      proteineDay       = json['proteineDay'],
                      blocchiZona       = json['blocchiZona'],
                      blocchimin        = json['blocchimin'],
                      queryStatus       = json['queryStatus'];                  
                $("#percentualeMG").val(percentualeMG);
                $("#percentualeMM").val(percentualeMM);
                        $("#proteineDay").val(proteineDay);
                        $("#blocchiZona").val(blocchiZona);

                            //alert(json.queryStatus);
                      console.log('addProtNeed success');
            },
            complete:function(){
                //$noty.close();
                //myNoty("information","Caricamento completato");
                    //myNoty("success","Pasto caricato","300","");
                console.log('Calcolo complete');
            },
            error: function(){
                  console.log('error');
                }
        });

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM