weixin_33696106 2019-09-26 09:46 采纳率: 0%
浏览 38

获取拉相同ID的参数

I have search results on left. When clicking on a result it needs to load in div on right - without a refresh or loading in a new page. The search gives three search results and has pagination. Regardless of which search result I click, the same id loads. Can anyone see where I am going wrong?

index.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 } ?>

file.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条回答 默认 最新

  • csdnceshi62 2019-09-26 09:55
    关注

    Your code produces multiple loadDoc functions inside the loop, therefore every button uses the same function (with the same rowId statically placed inside of it).

    It would be better if you had only one function and pass the id as a parameter like so:

    <? 
    $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 } ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 maixpy训练模型,模型训练好了以后,开发板通电会报错,不知道是什么问题
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容