Joel Joel - 4 months ago 16
PHP Question

Page jumps after AJAX Call to PHP chat

So, when i press the "send button" the page jumps up to the top which should happen. Any ideas? I've tried animating and return false but nothing seems to work. JSFIDDLE: http://jsfiddle.net/xwyzs05v/2/

AJAX:

$(document).ready(function(){
var interval = setInterval(function(){
$.ajax({
url: 'chat.php',
success: function(data){

$('#messages').html(data);
}
});
},1000);

});

$('#formInput').submit(function(){

var message = $('#message').val();
var sender = $('#sender').val();

$.ajax({
url: 'send.php',
data: { sender: sender, message: message },
success: function(data){

}
$('#message').val('');
});

return false;
});

Answer

You have a syntax error because you write $("#message").val("") out of a success function.

Correct code:

$(document).ready(function(){
$('#formInput').submit(function(){

    var message = $('#message').val();
    var sender = $('#sender').val();

    $.ajax({
      url: 'send.php',
      data: { sender: sender, message: message },
      success: function(data){
            $('#message').val('');
      }

    });

    return false;
});


});
Comments