dongmu2026 2015-11-19 19:15 采纳率: 100%
浏览 13
已采纳

如何使用ajax检索不同的值?

I have 2 div box. The first div box contains the user names i have conversed before. The second div box will display the conversation history of me and the user....

Here are my codes..

<ul style="list-style-type: none;">
    <?php  
    include 'connectDB.php';
    //retrieve all the message history of yours from the PrivateMessage table
    $query = "SELECT * FROM `messagecheck` WHERE `sender_a` = '$userId' OR `sender_b` = '$userId';";
    $result = mysqli_query($dbconnect,$query);
    if (mysqli_num_rows($result) > 0) { 
        while ($row = mysqli_fetch_assoc($result)) {    
    ?>
    <div class="openChat" style="border:2px solid red">
        <li style="color:black; width:100%; padding-top:5px" onclick="message()">
            <?php 
            //if the sender_a id is not my id, display the name
            //if sender_a is my id, then display sender_b
            //Because we dont want to see our name in the chat History
            if ($row['sender_a'] != $userId) {
                $senderName = $row['sender_a'];
                include 'connectDB.php';
                $nameSearch = "SELECT `fName`, `lName` FROM `register` WHERE `id`='$senderName';";
                $Searchresult = mysqli_query($dbconnect, $nameSearch);
                $Searchrow = mysqli_fetch_assoc($Searchresult);
                echo $Searchrow['fName'] ." ". $Searchrow['lName'];
            ?>
            <input class="IDValue" name="IDValue" value="<?php echo $row['exist']?>" >
            <?php
            } else {
                $senderName = $row['sender_b'];
                include 'connectDB.php';
                $nameSearch = "SELECT `fName`, `lName` FROM `register` WHERE `id`='$senderName';";
                $Searchresult = mysqli_query($dbconnect,$nameSearch);
                $Searchrow = mysqli_fetch_assoc($Searchresult);
                echo $Searchrow['fName'] ." ". $Searchrow['lName'];
            ?>
            <input class="IDValue" name="IDValue" value="<?php echo $row['exist']?>" >
            <?php
            } 
            ?>
       </li>
    </div>
    <?php 
        }
    }
    ?>
</ul>

In this code above, Each names will be displayed in a li list. Now when i click that a li list, lets say I am clicking the 3rd li and the id is supposed to be 34, it wont happen. It will still load the first li's Id... I cant find a fix.

Here is the code of my ajax :

function message(){
                var ID = document.querySelector('.IDValue').value;
                var xmlhttp = new XMLHttpRequest();
                    xmlhttp.onreadystatechange = function(){
                        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                           document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                        }
                    };
                    xmlhttp.open("POST","retrieveMsg.php?q=" +ID,true);
                    xmlhttp.send();
                }

Can somebody help me out please ? Im really tired of fixing it. Thanks it advance !

P.S : It normally shows(a chrome dev tool) http://localhost/html/retrieveMsg.php?q=16 nomatter which li i click (16 is the first li tag btw)

  • 写回答

3条回答 默认 最新

  • doukuang6795 2015-11-19 19:27
    关注

    You are always getting 16 because of your selector .IDValue ( it always grabs the first match which in your case is li 16 ).

    If you are dead-set on having your Javascript inline, you could pass the ID through the message function or store the id on the li itself using data-id="<?php echo $row['exist']?>":

        <li style="color:black; width:100%; padding-top:5px" 
    onclick="message(<?php echo intval($row['exist']);?>)">
    
    function message(clickedID){
                    var ID = clickedID;
                    var xmlhttp = new XMLHttpRequest();
                        xmlhttp.onreadystatechange = function(){
                            if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
                               document.getElementById("txtHint").innerHTML = xmlhttp.responseText;
                            }
                        };
                        xmlhttp.open("POST","retrieveMsg.php?q=" +ID,true);
                        xmlhttp.send();
                    }
    

    PS: You should look into PDO for fetching your mysql data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!