I have the below codes but anytime I run it, the data is not sent to the PHP file.
Here is the ajax code:
$.ajax({
type: "POST",
url: "api/tired.php",
data: { username: username },
success: function (data) {
$("#msg").html(data);
},
});
Here is the php file:
if(isset($_POST['username'])) {
echo $_POST['username'];
}
else {
echo "hello bro";
}
Instead of displaying the username, it is displaying ‘hello bro’.
Can someone please help?
2
Answers
The script is actually correct. I got to realize that my serviceworker.js file was overriding the referrer.
So I had to reconfigure my serviceworker.js file and it worked perfectly fine.
Thanks, everyone.
Apparently it is difficult to understand with just that snippet on what you have done (or maybe I just don’t have a debugger’s eye). I just want to confirm, is that the only code that you have for sending the "username" to the outstream?
What I recommend doing is:
Check if the input has the data for username in it. If not then it makes sense why "Hello bro" is getting printed.
Generally when I send data to frontend via outstream, it’s like:
Check again with the data by putting single inverted-s. Sometimes, I’ve seen even when the code is correct such small things F it up.