dongpigui8898 2018-12-13 10:40
浏览 89
已采纳

如何使用ajax帖子和数据表附加数据不起作用?

enter image description here

Hello there am trying to get the data from database using ajax posts but i didn't get any data properly. first column data is splinting in another columns(Member names are coming in image field and info field). Image also shared please check that. And also datatables are not working while fetching the data using Ajax. Help me out from this problem...

Thanks & Regards

<body>
      <label>Party</label>  
      <select id='partydropdown' name='partydropdown' onchange="partyFunction();">
      <option>--select a party--</option>
        <?php
            if ($result->num_rows > 0) {
            while ($row = $result->fetch_assoc()) {
        ?>
        <option value="<?php echo $row["Id"];?>"> 
            <?php echo $row["PartyName"];?>
        </option>
        <?php }}?>  
    </select>
    <div id="showhide"></div>   
    </body>
    <script type="text/javascript">
    function partyFunction(){
    debugger;
     $("#showhide").empty();
     $("#showhide").html('');
     $("#showhide").append("<table class='table table-bordered text-center table-responsive' border='1px' id='example'>"+
        "<tr>"+  
        "<th>PartyMemberName</th>"+
        "<th>Image</th>"+
        "<th>Info</th>"+
        "</tr>"+
        "<tbody id='partyBody'>"+
         "</tbody>"+
        "</table>"
        );
    $postdata = {};
    $postdata["Id"]=$("#partydropdown").val();
    console.log($("#partydropdown").val());
    $.post('test_data.php',$postdata,function (data) {
        debugger;
        console.log(data);
        console.log(data["data"][0].candiateName);
     $("#partyBody").empty();
     $("#partyBody").html('');
     console.log(data["data"]);
     console.log(data["data"].length);
      for(var i=0; i<data["data"].length; i++){
      if(data["data"][i].candiateName != null){ 
      $("#partyBody").append("<tr>"+
              "<td id='resdata"+i+"'></td>"+
              "<td id='resdata1"+i+"' ></td>"+
              "<td id='resdata2"+i+"'></td>"+             
          "</tr>");
           $("#resdata"+i).text(data["data"][i].candiateName);
          $("#resdata1"+i).append("<img id='photo"+i+"'>");
          $("#resdata2"+i).text(data["data"][i].Background);         
          $("#photo"+i).attr('src', 'http://aptsvotes.com/wp-content/themes/apts2019/img'+data["data"][i].Photo );        
        }
    }
    });
    };
    </script>

here is the test_data.php code

  <?php
    include_once "conn.php";
    include_once "voterdbclass.php";
    session_start();
    $tbl_name2="Parties";
    $dbObj = new Database1();
    $values1 = array("all");
    $querys = "SELECT c1.CandidateName,c1.Photo,c1.Background ,c1.Type FROM aptsv1_votes.Parties p1 LEFT JOIN aptsv1_votes.Candidates c1 ON c1.CurrentPartyId = p1.Id where p1.Id ='" . $_POST['Id'] . "' limit 21";
    $res = $dbObj->SelectRecord($tbl_name2,$values1,"","$querys");
    $data=array();
    $i=0;
    while ($rs = $res->fetch_array(MYSQLI_ASSOC)) { 
      $data[$i]['candiateName']=$rs['CandidateName'];
      $data[$i]['Photo']=$rs['Photo'];
      $data[$i]['Background']=$rs['Background'];
      $i++;
    }
    $json_array= array(
    "data" =>$data
    );
    echo json_encode($json_array);
    ?>
  • 写回答

2条回答 默认 最新

  • dqc3469 2018-12-13 10:52
    关注
    for(var i=0; i<data["data"].length; i++){
          if(data["data"][i].candiateName != null){ 
              var imge = data["data"][i].Photo;
              var name = data["data"][i].candiateName;
              var bg = data["data"][i].Background;
          $("#partyBody").append("<tr>"+
              "<td id='resdata" + i + "'>" + name+"</td>"+
              "<td id='resdata1" + i + "' ><img id= 'photo" + i + "' src='http://aptsvotes.com/wp-content/themes/apts2019/img'" + imge+"></td>"+
              "<td id='resdata2" + i + "'>" + bg+"</td>"+             
              "</tr>");
    
            }
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 运筹学排序问题中的在线排序
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败
  • ¥20 有人能用聚类分析帮我分析一下文本内容嘛