0

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('');
        
    } 
        
    });
    
    
    
}
    

});

});

6
  • 2
    what is the "alert('hello');" doing in your ajax config object? its destroying the syntax. Commented Jul 8, 2020 at 11:08
  • anyway it does not work either I tried the alert in the success function instead ajax Commented Jul 8, 2020 at 11:20
  • and you do have jquery included and a textarea or input[type=text] with the id text ? Commented Jul 8, 2020 at 11:25
  • yes man everything is ok Its a textarea type = text and id =text Commented Jul 8, 2020 at 14:37
  • then you got to provide us with more of your code, since the snippet above is fine. Commented Jul 8, 2020 at 15:07

2 Answers 2

0

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">

Sign up to request clarification or add additional context in comments.

4 Comments

it still seems doesn't
Can you click on Run Code snippet in the above ans, you should be able to see that it is entering into ajax and keyup event is functioning. And also can you share the console error that you see when you run in local?
Uncaught TypeError: $.ajax is not a function at HTMLTextAreaElement.<anonymous> (sent_messages.php:92) at HTMLTextAreaElement.dispatch (jquery-3.4.1.slim.min.js:2) at HTMLTextAreaElement.v.handle (jquery-3.4.1.slim.min.js:2) (anonymous) @ sent_messages.php:92 dispatch @ jquery-3.4.1.slim.min.js:2 v.handle @ jquery-3.4.1.slim.min.js:2
yes I know its getting the keyup event but I just noticed the error in the console its the problem in ajax or my Jquery version I dont know help me
0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.