skip to Main Content

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


  1. Chosen as BEST ANSWER

    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.


  2. 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:

    1. Check if the input has the data for username in it. If not then it makes sense why "Hello bro" is getting printed.

    2. Generally when I send data to frontend via outstream, it’s like:

       if(isset($_GET["userName"])) { 
         $someVariable = $_GET["userName"];
       }
       $paging= $someVariable;   //if its a list then getList($someVariable)
       echo $paging->toJson();
      
    3. 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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search