Ajax jQuery从先前的查询中检索数据

时间:2015-05-17 15:26:14

标签: javascript php jquery mysql ajax

我目前正在使用Ajax& jQuery用于我的聊天,有些人可能会说它愚蠢复杂,但只要我能让它工作。

它适用于第一个朋友的结果,但另一方面却没有。它的作用是chat-load.php(Ajax部分)创建一个新的查询来选择朋友的详细信息,然后将其放入chat.php页面的结果查询中。

因此它只显示一个"工作"第一个朋友的聊天窗口。我尝试从chat.php查询中获取上一个friend_id并在chat-load.php查询中使用它,但它似乎没有注意到任何数据。

这是我得到的一个例子,第一个结果全宽意味着它的工作,其他结果不是全宽,因为不使用ajax。

enter image description here

这是我目前的设置:

chat.php

<?php $users = $db->query("SELECT DISTINCT users.id, users.firstname, users.lastname, 
    message.date, message.time, message.message, message.recipient, message.sender
FROM users 
JOIN friends
    ON users.id IN (friends.sender, friends.recipient)
JOIN message 
    ON (users.id = message.sender)
AND 75 IN (message.sender,message.recipient)
ORDER BY message.date DESC, message.time DESC");   ?>




                <!-- Friends query -->


<?php $users = $db->query("SELECT IF(friends.sender = ".$_SESSION["user"]["id"].", friends.recipient, friends.sender) AS user_id
FROM friends
WHERE friends.sender = ".$_SESSION["user"]["id"]."
    OR friends.recipient = ".$_SESSION["user"]["id"]."");   ?>


 <?php while($friend = $users->fetch_object()): ?>

<?php  $friends = $db->query("SELECT firstname, lastname, id FROM users WHERE id = $friend->user_id "); ?>
 <?php while($FriendName = $friends->fetch_object()): ?>



        <div class="chat-box">
            <div class="header">
<?= $FriendName->firstname ?> <?= $FriendName->lastname ?>  <?= $FriendName->id ?>        

            </div>

                <script>
$(window).load(function() {
  $("#chat-box, #messages").animate({ scrollTop: $(document).height() }, 1000);
});
</script>
   <script>
function loadlink(){
    $('#messages').load('chat-load.php',function () {
         $(this).unwrap();
    });
}

loadlink(); // This will run on page load
setInterval(function(){
    loadlink() // this will run after every 5 seconds
}, 100);
</script>


            <div id="messages" class="messages">



            </div>

            <div class="input-box">
                    <form id="SendForm" class="SendMsg" role="form" method="post">
   <input type="text" id="s_firstname" name="s_firstname" class="MsgInputHidden" value="<?= $_SESSION["user"]["firstname"] ?>" />
   <input type="text" id="s_lastname" name="s_lastname" class="MsgInputHidden" value="<?= $_SESSION["user"]["lastname"] ?>" />
   <input type="text" id="sender" name="sender" class="MsgInputHidden" value="<?= $_SESSION["user"]["id"] ?>" />
   <input type="text" id="r_firstname" name="r_firstname" class="MsgInputHidden"value="<?= $FriendName->firstname ?>" />
   <input type="text" id="r_lastname" name="r_lastname" class="MsgInputHidden"value="<?= $FriendName->lastname ?>" />
   <input type="text" id="recipient" name="recipient" class="MsgInputHidden" value="<?= $FriendName->id ?>" />
      <input type="text" id="ip" name="ip" class="MsgInputHidden" value="<?= $_SERVER["REMOTE_ADDR"] ?>" />
      <input type="text" id="date" name="date" class="MsgInputHidden" value="<?= date('Y-m-d') ."\n" ?>" />
      <?php
$now = time(); 
$utc_time = $now - intval(date('Z', $now)); 

?>

      <input type="text" id="time" name="time" class="MsgInputHidden" value="<?= '' . date('H:i:s', $now) . '' ?>" />

                    <input id="message" type="text" name="message" style="width: 100%; overflow: scroll;">
                       <input id="submit" class="submit" type="submit" name="submit" value="Submit" />

                    </form>






            </div>
        </div>
            <script>
$("#submit").click(function(e) {
  e.preventDefault();
  var message = $("#message").val();
var s_firstname = $("#s_firstname").val();
var s_lastname = $("#s_lastname").val();
var sender = $("#sender").val();
var r_firstname = $("#r_firstname").val();
var r_lastname = $("#r_lastname").val();
var recipient = $("#recipient").val();
var ip = $("#ip").val();
var date = $("#date").val();
var time = $("#time").val();
var dataString = '&message=' + message + '&s_firstname=' + s_firstname + '&s_lastname=' + s_lastname + '&sender=' + sender + '&r_firstname=' + r_firstname + '&r_lastname=' + r_lastname + '&recipient=' + recipient + '&ip=' + ip + '&date=' + date + '&time=' + time;  
$.ajax({
    type:'POST',
    data:dataString,
    url:'sendmessagefriend.php',

  });
});

</script>
<script>
$("#submit").click(function(e) {
$("#SendForm").get(0).reset();
});
</script>
<script>
$("#submit").click(function(e) {
  $("#chat-box, #messages").animate({ scrollTop: $(document).height() }, 1000);
});
</script>
         <?php endwhile; ?> 

                 <?php endwhile; ?> 

聊天load.php

<!DOCTYPE html>
<html>

<head>
  <?php
    session_start();
if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])
)

      // redirect to index page if not superuser
header('Location: index.php');
?>
</head>
<body>
  <?php
$q = intval($_GET['q']);

$con = mysqli_connect('localhost','****','****','****');
if (!$con) {
    die('Could not connect: ' . mysqli_error($con));
}

mysqli_select_db($con,"ajax_demo");

$sql="SELECT message, sender, recipient, date, time, IF(recipient = ".$_SESSION["user"]["id"].", 'received', 'sent') AS direction FROM message WHERE recipient IN (".$_SESSION["user"]["id"].", ".$friend->user_id.") AND sender IN (".$_SESSION["user"]["id"].", ".$friend->user_id.")";
$result = mysqli_query($con,$sql);
while($row = mysqli_fetch_array($result)) {
    echo "<ul>";
    echo "<li>";
    echo "<span class='". $row['direction'] ."'>". $row['message'] ."</span>";
    echo "<div class='clear'></div>";
    echo "</li>";
    echo "</ul>";


}
mysqli_close($con); ?>
</body>
</html>

1 个答案:

答案 0 :(得分:1)

尝试使用$.ajax()代替$.load()。设置cache: false

示例:

function loadlink(){
    $.ajax({
        url:'chat-load.php',
        cache: false,
        success: function(result){
             $('#messages').html(result);
        }
    });
}