dongwusang0314 2016-08-05 02:58
浏览 66

Javascript,PHP,AJAX:如何回显数组并将它们附加到html部门

I am a beginner and I am trying to echo arrays and append them into a html division i created. The AJAX call I made was successful but the response was not appearing inside the division i assigned it to. Upon further inspection, I found out that the response I have received has an array length over 600 ( I am expecting 10 records). So there must be something wrong with my echo PHP file or the receiving end of the HTML file but just cannot figure out what that is.

Here are my codes:

listdiscount.php

<?php 
header("Access-Control-Allow-Origin: *"); 
header("Content-Type: application/json; charset=UTF-8"); 

$conn = new mysqli("localhost", "root", "root", "bencoolen");

$userid = $_GET['userid'];

$result = $conn->query("select userid, codename from discountcode where userid ='" . $userid . "' ");

 while($row = mysqli_fetch_array($result, MYSQLI_ASSOC)){
     printf("Userid: %s '' Codename: %s", $row["userid"], $row["codename"]);
 }

 $conn->close(); 
?>

index.html ( with js codes )

<html>
<script>
    function mydiscount(){
                          var userid = "jimmy"; 
                            var xmlhttp = new XMLHttpRequest();
                            var url = serverURL() + "/listdiscount.php";
                            url += "?userid=" + userid; 
                            alert(url);
                            xmlhttp.onreadystatechange=function() {
                                if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                                    alert("readystate=4 and status =200");
                                    mydiscountresult(xmlhttp.responseText);
                                }
                                }
                                xmlhttp.open("GET", url, true);
                                xmlhttp.send();
                        }

                        function mydiscountresult(response) {
                            var arr = response;
                            alert(arr);         //alerted
                            alert(arr.length);  //alerted 600+ arrays
                            alert("mydiscountresult working"); //alerted
                            var i;
                            $("#discountcontent").empty();
                            for(i = 0; i < arr.length; i++) {
                            $("#discountcontent").append("<b>" + arr[i].userid + "</b>"+ arr[i].codename + "<hr>");
                            }
                        }

                        mydiscount();
</script>

                    <div data-role="content" class="ui-content" id="discount">
                    LIST OF DISCOUNT CODE:<br>
                    <div id="discountcontent" class="ui-grid-solo">
                    </div>
                    </div>
</html>

Here is what my response look like when alerted:

addition note: my php files are in different folder so serverurl() is declared and used here.

  • 写回答

2条回答 默认 最新

  • douling6469 2016-08-05 04:28
    关注

    the get request returns a json array so you need to convet it in to normal array using json parse. you may please change the line of code as

       mydiscountresult(JSON.parse(xmlhttp.responseText));
    

    It will work fine.

    评论

报告相同问题?

悬赏问题

  • ¥15 2024-五一综合模拟赛
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭