简体   繁体   English

设置从ajax请求到mysql数据库的javascript变量

[英]Set javascript variables from ajax request to mysql database

I am trying to make an ajax request to a mysql database and need to loop through the results and either set them as javascript variables or use them in functions within the loop. 我正在尝试向mysql数据库发出ajax请求,并且需要遍历结果并将它们设置为javascript变量或在循环内的函数中使用它们。

mysql query where id is not unique and will return an array of results: mysql查询,其中id不是唯一的,并且将返回结果数组:

    $id =$_REQUEST['id'];
    $sth = $conn->prepare("SELECT * FROM quote where id = :id");
    $sth->bindParam(':id', $id);
    $sth->execute();
    $result = $sth->fetchAll();
    echo json_encode($result);

Ajax request using post and log results: 使用发布和日志结果的Ajax请求:

$.ajax({     
    type: "POST",
    data: "id="+id,
    url: "get-quote-data.php",
    cache: false,
    success: function (result) { 
        console.log(result);
      }
   });  

logs 2 objects in an array as there are currently 2 results: 将2个对象记录在数组中,因为当前有2个结果:

    [{"id":"6776t2dhc3aq","0":"6776t2dhc3aq","Wid":"6776t2dhc3aqnew0.5981985541579144","1":"6776t2dhc3aqnew0.5981985541579144","room":"Plot 1 Bedroom","2":"Plot 1 Bedroom","style":"S150","3":"S150","type":"upvc","4":"upvc","colour":"0","5":"0","RAL":"","6":"","cill":"2","7":"2","width":"1023.0000","8":"1023.0000","height":"1585.0000","9":"1585.0000","fitting":"1","10":"1","comments":"","11":"","spacercol":"1","12":"1","glassstyle":"0","13":"0","handles":"0","14":"0","hinges":"0","15":"0","trickle":"0","16":"0","sash":"0","17":"0","fanlight":"0","18":"0","total":"0.0000","19":"0.0000","g1":"6","20":"6","g2":"6","21":"6","g3":"0","22":"0","g4":"0","23":"0","g5":"0","24":"0","g6":"0","25":"0","g7":"0","26":"0","g8":"0","27":"0","m1":"1023.0000","28":"1023.0000","m2":"1023.0000","29":"1023.0000","m3":"0.0000","30":"0.0000","m4":"0.0000","31":"0.0000","m5":"0.0000","32":"0.0000","m6":"0.0000","33":"0.0000","m7":"0.0000","34":"0.0000","m8":"0.0000","35":"0.0000","t1":"777.5000","36":"777.5000","t2":"777.5000","37":"777.5000","t3":"0.0000","38":"0.0000","t4":"0.0000","39":"0.0000","t5":"0.0000","40":"0.0000","t6":"0.0000","41":"0.0000","t7":"0.0000","42":"0.0000","t8":"0.0000","43":"0.0000","ICwinBor":"0","44":"0","LOF":"0","45":"0","LOH":"0","46":"0","intFinish":"0","47":"0","fEx":"0","48":"0","fExt":"0","49":"0","fExl":"0","50":"0","fExr":"0","51":"0","cler":"0","52":"0","fMul":"0","53":"0","commission":"666.999666","54":"666.999666","survey":"666.999666","55":"666.999666","NumItem":"1","56":"1","slideType":"","57":"","horns":"0","58":"0","vsLS":"0","59":"0","vsArched":"0","60":"0","vsFixed":"0","61":"0","vsSecure":"0","62":"0","vsPolished":"0","63":"0","vsJointed":"0","64":"0","vsBay":"0","65":"0","UPVCfStyle":"1","66":"1","UPVCbStyle":"1","67":"1","ALUfStyle":"1","68":"1","ALUbStyle":"1","69":"1","drain":"1","70":"1","ventPos":"1","71":"1","ALUProfile":"Alitherm 600","72":"Alitherm 600"},
{"id":"6776t2dhc3aq","0":"6776t2dhc3aq","Wid":"6776t2dhc3aqnew0.8957906831392683","1":"6776t2dhc3aqnew0.8957906831392683","room":"Plot 2 Bedroom","2":"Plot 2 Bedroom","style":"S150","3":"S150","type":"upvc","4":"upvc","colour":"0","5":"0","RAL":"","6":"","cill":"2","7":"2","width":"1023.0000","8":"1023.0000","height":"1585.0000","9":"1585.0000","fitting":"1","10":"1","comments":"","11":"","spacercol":"1","12":"1","glassstyle":"0","13":"0","handles":"0","14":"0","hinges":"0","15":"0","trickle":"0","16":"0","sash":"0","17":"0","fanlight":"0","18":"0","total":"0.0000","19":"0.0000","g1":"6","20":"6","g2":"6","21":"6","g3":"0","22":"0","g4":"0","23":"0","g5":"0","24":"0","g6":"0","25":"0","g7":"0","26":"0","g8":"0","27":"0","m1":"1023.0000","28":"1023.0000","m2":"1023.0000","29":"1023.0000","m3":"0.0000","30":"0.0000","m4":"0.0000","31":"0.0000","m5":"0.0000","32":"0.0000","m6":"0.0000","33":"0.0000","m7":"0.0000","34":"0.0000","m8":"0.0000","35":"0.0000","t1":"777.5000","36":"777.5000","t2":"777.5000","37":"777.5000","t3":"0.0000","38":"0.0000","t4":"0.0000","39":"0.0000","t5":"0.0000","40":"0.0000","t6":"0.0000","41":"0.0000","t7":"0.0000","42":"0.0000","t8":"0.0000","43":"0.0000","ICwinBor":"0","44":"0","LOF":"0","45":"0","LOH":"0","46":"0","intFinish":"0","47":"0","fEx":"0","48":"0","fExt":"0","49":"0","fExl":"0","50":"0","fExr":"0","51":"0","cler":"0","52":"0","fMul":"0","53":"0","commission":"666.999666","54":"666.999666","survey":"666.999666","55":"666.999666","NumItem":"1","56":"1","slideType":"","57":"","horns":"0","58":"0","vsLS":"0","59":"0","vsArched":"0","60":"0","vsFixed":"0","61":"0","vsSecure":"0","62":"0","vsPolished":"0","63":"0","vsJointed":"0","64":"0","vsBay":"0","65":"0","UPVCfStyle":"1","66":"1","UPVCbStyle":"1","67":"1","ALUfStyle":"1","68":"1","ALUbStyle":"1","69":"1","drain":"1","70":"1","ventPos":"1","71":"1","ALUProfile":"Alitherm 600","72":"Alitherm 600"}]

Within my success function I need to be able to loop through all of the results in each object and use them as I require. 在我的成功函数中,我需要能够遍历每个对象中的所有结果,并根据需要使用它们。

I have tried many of the answers posted on here with no luck, such as: 我尝试过许多没有运气的答案,例如:

for (var j = 0; j < result.length; j++){
        var windowid = result[j].Wid);
        console.log(windowid );
     }

logs undefined. 日志未定义。 I have also tried many for in and foreach loop examples with no luck. 我也尝试过很多for和ineach循环示例,但没有运气。 Thanks in advance for any help. 在此先感谢您的帮助。

Parse the result as JSON before the for loop. 在for循环之前将结果解析为JSON。 Add line result = JSON.parse(result) . 添加result = JSON.parse(result) As content-type header is not set to application/json your result will be a string which should be parsed as json. 由于未将content-type标头设置为application/json因此您的结果将是一个字符串,应将其解析为json。

First, you're returning in JSON format, so set that in the ajax request... 首先,您以JSON格式返回,因此请在ajax请求中进行设置...

$.ajax({     
    type: "POST",
    data: "id="+id,
    url: "get-quote-data.php",
    dataType: 'json',
    cache: false,
    success: function (result) { 
        console.log(result);
    }
});

Inside the success function, you can use $.each function to loop through your response... 在成功函数中,您可以使用$ .each函数循环响应...

$.each(result,function(id,value) {
    value['id']...
    value['Wid']...
    etc...
});

So, putting all together... 所以,放在一起...

$.ajax({     
    type: "POST",
    data: "id="+id,
    url: "get-quote-data.php",
    dataType: 'json',
    cache: false,
    success: function (result) {

        $.each(result,function(id,value) {
            var windowid = value['Wid'];
            console.log(windowid);
        });
    }
});

I hope it helps 希望对您有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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