开发者

Submit an ajax/jquery form without refreshing - INPUT = #msg, OUTPUT = #messages

开发者 https://www.devze.com 2023-03-25 03:12 出处:网络
I basically have a form that you input chat messages into. It looks like chat roulette/omegle. You see yourself as YOU, and the 开发者_开发技巧other person as STRANGER. I need to be able to reload the

I basically have a form that you input chat messages into. It looks like chat roulette/omegle. You see yourself as YOU, and the 开发者_开发技巧other person as STRANGER. I need to be able to reload the messages without refreshing the page when submitted. This is at the bottom of the HTML page:

<script type="text/javascript">
$(document).ready(function(){
var refreshchat = function(){
    $.get('ajax.php', function(data) {
        $('#messages').html(data);
    });
    var msgs = document.getElementById('messages');
    msgs.scrollTop = msgs.scrollHeight;
}
var getmsgs = self.setInterval(refreshchat,0);
$('#send').click(function(){
    var msg = $('#msg').val();
    $.post("ajax.php", { msg: msg, from: "You" },
       function(data) {
            $('#messages').html(data);
            $('#msg').val('');
            var msgs = document.getElementById('messages');
            msgs.scrollTop = msgs.scrollHeight;
    });
});
});

</script>

I saw this question/answer on stackoverflow, but am not sure how to make it work with this setup.


You need to create the send button. Edited: getmsgs is automatically called. Other than the button, the only thing I can think of is the ajax.php file. Is that created?

 <input type="button" id="send">
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号