Here's what I want to do.
I have a div
element that is being constantly refreshing (per every second) with the use of JQuery. And another JQuery code sends a post request to the same page when the user starts typing to a textarea
(not when submitted).
NOTE: Below snippet will not run because it contains PHP code.
// This code submits data to the same page with post.
$(document).ready(function(){
$("#msg").on("input", function(){
$.post( window.location, { isTyping: "yes" } );
});
});
// This is the code I use to refresh the div.
var timing = setInterval(Timer, 1000);
function Timer() {
$("#myId").load(location.href + " #myId");
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div id="myId">
<?php
if (isset($_POST['isTyping'])) {
if ($_POST['isTyping'] == "yes") {
echo "Typing";
}
}
?>
</div>
<!-- This is the textarea -->
<textarea id="msg"></textarea>
When the div
refreshes, the echo
must print "Typing". But, it doesn't. To check if the data is being submitted, I tried creating a new text file and inserting value to it (omitting the echo
), and it worked. The fopen/fwrite
command outputted text to a file (with the data received through post
), but the echo
is not giving any output on the screen.
I don't know why this happens, tried quite a few hours searching the whole Google and SO, either I didn't know what to search for or didn't find what I wanted. I am very new to JQuery, Sorry about that.