dsbiw2911188 2018-07-11 10:32
浏览 86
已采纳

无法使用PHP生成的Bootstrap选项卡中的AJAX检索数据

So i have a select box which triggers an AJAX call to retrieve information from MYSQL which displays Bootstrap tabs. I'm trying to do another call to AJAX to display content in every tabs based of their value but can't seem to get a result back.

Here is part of the code that generates the tabs:

$result= $conn->query($sql);
if ($result->num_rows > 0) {
echo"
<div id=\"tabs\">
    <ul class=\"nav nav-tabs\">";
    while($row = $result->fetch_assoc()) {
    echo"
        <li class=\"nav-item\">
        <a class=\"nav-link\" id=\"gruppo\" value=\"".$row["id_gruppo"]."\" data-toggle=\"tab\" href=\"#".$row["id_gruppo"]."\">".$row["nome_gruppo"]."</a>
        <div id=\"show1\"></div>
        </li>";
      }
    echo"</ul></div>";
}

This is the AJAX code that i came with:

<script type="text/javascript">
  $(document).ready(function(){ 
    $("#gruppo").change(function(){ 
      var id_gruppo = $(this).val(); 
      var dataString = "id_gruppo="+id_gruppo; 

      $.ajax({ 
        type: "POST",
        url: "getData.php",
        data: dataString,
        success: function(result){
          $("#show1").html(result);
        }
      });

    });
  });
</script>

And just to test if anything happens this is the code of the page getData.php

 if(!empty($_POST["id_gruppo"]))
 {
  $id_gruppo=$_POST["id_gruppo"];
  echo"Gruppo:".$id_gruppo;
 }
  • 写回答

3条回答 默认 最新

  • dongyan4424 2018-07-12 06:38
    关注

    Thanks to @Mabrouki Fakhri i found the solution.

    function that gets called for every tab:

    <script>
       function GetDataFromDB(id_gruppo){
            $.ajax({ /* THEN THE AJAX CALL */
                type: "POST", /* TYPE OF METHOD TO USE TO PASS THE DATA */
                url: "getData.php", /* PAGE WHERE WE WILL PASS THE DATA */
                data: "id_gruppo="+id_gruppo, /* THE DATA WE WILL BE PASSING */
                success: function(result1){ /* GET THE TO BE RETURNED DATA */
                $("#home").html(result1); /* THE RETURNED DATA WILL BE SHOWN IN THIS DIV */
            }
        });
    }
    </script>
    

    code that generatates every tab:

      if ($result->num_rows > 0) {
            echo"
            <div id=\"tabs\">
                <ul class=\"nav nav-tabs\">";
            while($row = $result->fetch_assoc()) {
            echo"
                    <li class=\"nav-item\">
                        <a class=\"nav-link\" id=\"gruppo\" onclick=\"GetDataFromDB(".$row["id_gruppo"].");\" value=\"".$row["id_gruppo"]."\" data-toggle=\"tab\" href=\"#".$row["id_gruppo"]."\">".$row["nome_gruppo"]."</a>
                    </li>";
          }
                echo"
                    </ul>
                </div>";
        }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!