Home > OS >  Jquery Ajax not submitting to PHP page
Jquery Ajax not submitting to PHP page

Time:01-05

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},
            /*beforeSend: function() {
              
              showloader();
            },*/
            success: function (data) {
              $("#msg").html(data);
            },
          });
                

Here is the php file

<?php
if(isset($_POST['username'])) {

    echo $_POST['username'];

} else {

    echo "hello bro";
}

instead of displaying the username, it is displaying 'hello bro'.

can someone pls help?

CodePudding user response:

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.

CodePudding user response:

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.

  • Related