在laravel 5.4中刷新聊天部分

时间:2017-06-19 11:16:04

标签: php ajax laravel-5 laravel-5.4

我在聊天系统工作,我需要刷新聊天部分以显示用ajax发送的新消息,这是我的消息刀片

 <div class="container">
        <div class="row">
            <div class="col-md-8 col-md-offset-2">
                <div class="panel panel-default">
                    <div class="panel-heading">messenger</div>

                    <div class="panel-body">
                        @if($array)
                            @foreach ($array as $message)
                                @foreach ($message->messages_reply as $reply)
                                    <li>  {{ $reply['msg']}} </li>
                                @endforeach
                            @endforeach
                        @endif
                        <br >
                        <form method="post" action="{{url("sendMessage")}}">
                            {{ csrf_field() }}
                            <input type="hidden" name="user_from" value="{{$from}}">
                            <input type="hidden" name="user_to" value="{{$to}}">
                            <input type="hidden" name="date" value="{{time()}}">
                            <input type="text" class="form-control" name="msg">
                            <br >
                            <input type="submit" value="send" class="btn btn-primary">
                        </form>
                    </div>
                </div>
            </div>
        </div>
    </div> 

1 个答案:

答案 0 :(得分:0)

使用ajax提交新邮件。

在提交按钮中添加1个ID

<input type="submit" id="sendMessage" value="send" class="btn btn-primary" />

在您的刀片文件中添加此ajax

$("body").on("click","#sendMessage",function(e){
                e.preventDefault();
                var user_from= $("#user_from").val();
                var user_to= $("#user_to").val();
                var date= $("#date").val();
                var msg= $("#msg").val();
                $.ajax({
                    type:"POST",
                    url:"{{url('sendMessage')}}",
                    data: {"user_from": user_from,"user_to": user_to,"date":date,"msg":msg},
                    success:function(res){
                       // Reload the page as you wanated
                        window.location.reload();
                    }
                });
            });