skip to Main Content

when I put alert before ajax call alert shows but doesn’t show inside the ajax call its not even getting inside the ajax call why its a php file and i’m using xxamp.

<div class="col-sm-12 pb-3">

<textarea. name="in" id="text" type="text" cols="30" rows="2" class="form-control" placeholder="Write a message…">

 <script src="https://code.jquery.com/jquery-3.4.1.slim.min.js" integrity="sha384-J6qa4849blE2+poT4WnyKhv5vZF5SrPo0iEjwBvKU7imGFAV0wwj1yYfoRSJoZ+n" crossorigin="anonymous"></script>
<script>

document.querySelector("#scroll_msg").scrollTop = document.querySelector("#scroll_msg").scrollHeight;

</script>





$(document).ready(function(){

$(‘#text’).keyup(function(e){

if(e.keyCode==13){   
    
var text=$('#text').val(); 
    $.ajax({
        
        type:'POST',
        url:'insert_msg.php',
    data:{text:text},
    
    success:function(){ 
        //alert('hello');
        $('#scroll_msg').load('display_msg.php'); 
        $('#text').val('');
        
    } 
        
    });
    
    
    
}
    

});

});

2

Answers


  1. Chosen as BEST ANSWER

    Yeah I finally got it nothing wrong with my code it's the slim minified version of Jquery caused the problem, so I replaced it with only minified it works now, thanks all.


  2. Remove alert() and following is the working code. (Obviously it will not work here as those endpoints are not valid for stackoverflow, but if you try in your environment, they should work)

    $(document).ready(function()
    {
        $('#text').keyup(function(e){
            if(e.keyCode==13)
            {    
               console.log('Event keyUp on enter');
                var text=$('#text').val(); 
                $.ajax({
                 
                    type:'POST',
                    url:'insert_msg.php',
                    data:{text:text},
    
                    success:function(){    alert('hello');
                        $('#scroll_msg').load('display_msg.php');
                        $('#text').val('');
                    } 
                });
            }   
        });     
    });
    <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.0.0/jquery.min.js"></script>
    <input type="text" id="text">
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search