Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/286.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 获取相同id的参数_Javascript_Php_Ajax - Fatal编程技术网

Javascript 获取相同id的参数

Javascript 获取相同id的参数,javascript,php,ajax,Javascript,Php,Ajax,我在左边有搜索结果。当点击结果时,它需要在右边的div中加载,而不需要刷新或加载新页面。搜索将给出三个搜索结果,并进行分页。无论单击哪个搜索结果,都会加载相同的id。谁能看出我错在哪里 index.php //get rows $query = $db->query("SELECT * FROM posts ORDER BY id DESC LIMIT $limit"); if($query->num_rows > 0){ ?>

我在左边有搜索结果。当点击结果时,它需要在右边的div中加载,而不需要刷新或加载新页面。搜索将给出三个搜索结果,并进行分页。无论单击哪个搜索结果,都会加载相同的id。谁能看出我错在哪里

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 } ?>
//获取行
$query=$db->query(“按id从posts订单中选择*描述限额$LIMIT”);
如果($query->num_rows>0){?>
看法

函数loadDoc(){ var xhttp=newXMLHttpRequest(); xhttp.onreadystatechange=函数(){ if(this.readyState==4&&this.status==200){ document.getElementById(“demo”).innerHTML=this.responseText; } }; open(“GET”,“file/file.php?id=”,true); xhttp.send(); }
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>

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

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