为了账号安全,请及时绑定邮箱和手机立即绑定

获取参数拉相同的id

获取参数拉相同的id

慕标5832272 2021-10-29 17:06:06
我在左边有搜索结果。单击结果时,它需要在右侧的 div 中加载 - 无需刷新或加载新页面。搜索给出三个搜索结果并有分页。无论我单击哪个搜索结果,都会加载相同的 ID。谁能看到我哪里出错了?索引.php    //get rows    $query = $db->query("SELECT * FROM posts ORDER BY id DESC LIMIT $limit");    if($query->num_rows > 0){ ?>        <div class="posts_list">        <?php            while($row = $query->fetch_assoc()){                 $postID = $row['id'];        ?>            <div class="list_item"><h2><?php echo $row["title"] ?></h2><button type="button" onclick="loadDoc()" >View</button></div>            <div class="item_viewer"><p id="demo"></p></div>                <script type="text/javascript">                function loadDoc() {                  var xhttp = new XMLHttpRequest();                  xhttp.onreadystatechange = function() {                    if (this.readyState == 4 && this.status == 200) {                      document.getElementById("demo").innerHTML = this.responseText;                    }                  };                  xhttp.open("GET", "file/file.php?id=<?php echo $row['id'] ?>", true);                  xhttp.send();                }                </script>        <?php } ?>        </div>        <?php echo $pagination->createLinks(); ?>    <?php } ?>文件.php<body>  <div class="container">    <div class="box"><?PHP//create connection$connect = new mysqli($host, $dbUsername, $dbPassword, $dbname);if (mysqli_connect_error()) {die('Connect Error('. mysqli_connect_errno().')'. mysqli_connect_error());} else {$id = $_GET['id'];$id = mysqli_real_escape_string($connect,$id);$query = "SELECT * FROM `posts` WHERE `id`='" . $id . "'";$result = mysqli_query($connect,$query);while($row = mysqli_fetch_array($result)) {echo "Company: <b>" .$row['title']. "</b>";echo "<br/>";echo "ID: <b>" .$row['id']. "</b>";?><a href="<?PHP echo $row[''];}}?>"><button class="btn success">View</button></a>        <a href="" ><button class="btn default">Back</button></a>          </div>    </div></body>
查看完整描述

1 回答

?
慕慕森

TA贡献1856条经验 获得超17个赞

您的代码loadDoc在循环内生成多个函数,因此每个按钮都使用相同的函数(相同的 rowId 静态放置在其中)。


如果您只有一个函数并将 id 作为参数传递,那就更好了,如下所示:


<? 

$query = $db->query("SELECT * FROM posts ORDER BY id DESC LIMIT $limit");


if($query->num_rows > 0){ ?>

    <div class="posts_list">

    <?php

        while($row = $query->fetch_assoc()){ 

            $postID = $row['id'];

    ?>

        <div class="list_item">

            <h2><?php echo $row["title"] ?></h2>

            <!-- Pass Id into the function -->

            <button type="button" onclick="loadDoc(<?php echo $row['id'] ?>)" >View</button> 

        </div>

        <div class="item_viewer"><p id="demo"></p></div>

    <?php } ?>

    </div>

    <script type="text/javascript"> // Move JavaScript function out of loop, so that it only exists once in the document

        function loadDoc(rowId) { // Pass rowId as argument

            var xhttp = new XMLHttpRequest();

            xhttp.onreadystatechange = function() {

            if (this.readyState == 4 && this.status == 200) {

                document.getElementById("demo").innerHTML = this.responseText;

            }

            };

            xhttp.open("GET", "file/file.php?id=" + rowId, true);

            xhttp.send();

        }

    </script>

    <?php echo $pagination->createLinks(); ?>

<?php } ?>


查看完整回答
反对 回复 2021-10-29
  • 1 回答
  • 0 关注
  • 134 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信