使用PHP从数据库中通过id显示数据

时间:2016-10-28 15:35:24

标签: php sql phpmyadmin

我的名字是Boonsita。我有一些关于PHP代码的问题。 我有一个使用ajax的home.html文件调用showWord.php。当我们从home.html点击链接然后链接到word.html    但是,我需要通过home.html中的id在word.html中显示单词

我希望你能理解,比你这么多。 以下代码

home.html的

<body onload="showWords()">
<div class="container">
    <div id="showWord">
        <!--show data in this-->
    </div>
</div>

<script type="text/javascript" src="js/jquery-1.12.3.min.js"></script> 
<script  type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
 function showWords() {
      //alert ("Hello");
      $.ajax({    //create an ajax request php
        type: "POST",
        url: "http://172.28.80.227/senior-project/std-56/370-593/Demo/showWord.php",
        success: function(response){                    
            $("#showWord").html(response); 
            //alert(response);
        }
    });
};
</script>

showWord.php

<body>
  <div class="col-xs-12">
  <?php
      $sql = "SELECT * FROM words";
      $result = $conn->query($sql);

      if($result->num_rows > 0)
        {
        while($row = $result->fetch_assoc()) { ?>
              <ul class="list-group">
                    <a href="word.html?id=<?php echo $row['id']; ?>" class="list-group-item"><h1><?php echo $row["word"] ?></h1></a>
              </ul>
  <?php
      }
    }
      else
    {
      ?>
          <br><br>
          <div class="col-xs-12">
            <div class="alert alert-warning">
                <span class="glyphicon glyphicon-info-sign"></span> &nbsp; ไม่มีข้อมูลคำศัพท์
              </div>
          </div>
          <br><br>
          <?php
    }
  ?>
</div>

word.html

<div class="container">
    <div id="showWord">
        <!--show data in this-->
    </div>
</div>

<script type="text/javascript" src="js/jquery-1.12.3.min.js"></script> 
<script  type="text/javascript" src="js/bootstrap.min.js"></script>
<script type="text/javascript">
 function Word() {
     console.log($(".input_text"));
      //alert ("Hello");
      $.ajax({    //create an ajax request php
        type: "GET",
        url: "http://172.28.80.227/senior-project/std-56/370-593/Demo/word.php",
        success: function(response){                    
            $("#showWord").html(response); 
            //alert(response);
        }
    });
};
</script>

word.php

<div class="col-xs-12">
  <?php
      $sql = "SELECT * FROM words";
      $result = $conn->query($sql);

      if($result->num_rows > 0)
        {
        while($row = $result->fetch_assoc()) { ?>
              <ul class="list-group">
                    <a href="word.html?id=<?php echo $row['id']; ?>" class="list-group-item"><h1><?php echo $row["word"] ?></h1></a>
              </ul>
  <?php
      }
    }
      else
    {
      ?>
          <br><br>
          <div class="col-xs-12">
            <div class="alert alert-warning">
                <span class="glyphicon glyphicon-info-sign"></span> &nbsp; ไม่มีข้อมูลคำศัพท์
              </div>
          </div>
          <br><br>
          <?php
    }
  ?>
</div>

0 个答案:

没有答案