简体   繁体   English

显示消息而不重新加载页面

[英]Display message without reloading page

My problem is every time i want to show a new message i have to reload this page to show new messages but I wanna show new messages without reloading page can i achieve this from JQuery . 我的问题是每次我想要显示新消息时,我都必须重新加载此页面以显示新消息,但是我想显示新消息而不重新加载页面,我可以从JQuery实现JQuery Please help me 请帮我

This part is where the messages are shown 这部分是显示消息的地方

$message_query= mysql_query ("SELECT `from_id`, `message` FROM `messages` WHERE `group_hash`='$hash'"); 
  while($run_message = mysql_fetch_assoc($message_query))
      {
         $from_id = $run_message['from_id'];
         $message = $run_message['message'];
        $user_query = mysql_query("SELECT `username` FROM `users` WHERE    `id`='$from_id'");
  $run_user = mysql_fetch_array($user_query);
  $from_username = $run_user['username'];
                echo "<p><b>$from_username</b><br />$message</p>";// this is the place where the message is displayed
      }

This part is where i click the submit button to send the message 这部分是我单击提交按钮发送消息的地方

<form method="POST">
        <?php
        if(isset($_POST['message']) && !empty($_POST['message']))
    {
     $new_message = $_POST['message'];
        mysql_query("INSERT INTO `u313495632_test`.`messages` (`id`, `group_hash`, `from_id`, `message`) VALUES ('', '$hash', '$my_id', '$new_message');");
        header('Location: conversation.php?hash='.$hash);
    }

    ?>
    Enter Message :<br/>
    <textarea name="message" rows='4' cols='30'></textarea>
    <br><br>
    <input type="submit" value="send message" />
    </form>

This is how the page looks like 这是页面的样子

在此处输入图片说明

in your script write something like this: 在脚本中编写如下内容:

<script>
$(document).ready(function(){
    $('form').submit(function(e){
        e.preventDefault();
        $.ajax({
            type: 'POST',
            url: "yourPhpFile.php",
            data:{message:$('textarea[name="message"]').val()},
            async:true,
            success: function(result){
                //do something
            }
        });
   });
});
</script>

//此页面显示在这里,然后使用此jQuery var timeout = setInterval(reloadpage,5000); function reloadpage(){$('#page')。load('page.php');} //注意,此刷新每五秒钟翻一页

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM