Solution 1 :


this will work try getting data with file_get_contents('php://input') instead of using $_POST

i tried it with you code and it works

$value = file_get_contents('php://input');
$value = json_decode($value);
$phone = $value->{"phone_num"};
echo $phone;

I think this is a better approach

JS

const response = fetch('http://localhost:8888/project/my-script.php', {
        method: 'POST', // *GET, POST, PUT, DELETE, etc.
        headers: {
        'Content-Type': 'application/x-www-form-urlencoded',
        },
        body: new URLSearchParams({phone_num: 12345678}).toString()
  });

PHP

 $request_method = $_SERVER['REQUEST_METHOD'];
    if ( $request_method == 'POST' ){
          echo "This is working!!";
          echo($_POST["phone_num"]);
    }else {
          echo json_encode('No defined function for this method');
    }

Solution 2 :

Try using $_REQUEST instead of $_GET.

I know it is lame to use that. But even with ReactJS also, I am facing the same issue and I resolved it with this.

Solution 3 :

xhttp.open("POST", "http://localhost:8888/project/my-script.php", true);

In your above url it seems that you are sending a post request

$request_method = $_SERVER['REQUEST_METHOD'];
if ( $request_method == 'POST' ){
      echo "This is working!!";
      echo $_GET['phone_num'];
}else {
      echo json_encode('No defined function for this method');
}

And if your php function it seems that you are accessing the variable phone_num using the GLOBAL GET array.

Change it to post

$_POST['phone_num'];

Solution 4 :

use this

echo $_POST[‘phone_num’];

Instead of

echo $_GET[‘phone_num’];

Problem :

I have been trying to post data to my PHP script, which works fine for POSTMAN, shows the results correctly. But in while sending the data using AJAX in my html project, the data is not being sent/accepted by the PHP script, hence no show data.

Possible Problem: JSON data type is not working for PHP script, since param is not getting read by $_POST['phone_num']

HTML Code:

<!DOCTYPE html>
<html>
<body>

<h2>The XMLHttpRequest Object</h2>

<button type="button" onclick="loadDoc()">Request data</button>

<p id="demo"></p>

<script>
  function loadDoc() {
    var xhttp = new XMLHttpRequest();
    xhttp.onreadystatechange = function() {
      if (this.readyState == 4 && this.status == 200) {
        document.getElementById("demo").innerHTML = this.responseText;
      }
    };
    xhttp.open("POST", "http://localhost:8888/project/my-script.php", true);
    xhttp.setRequestHeader("Content-type", "application/json");
    xhttp.send('{ "phone_num" : "12345678" }');
  }
</script>

</body>
</html>

PHP Script:

$request_method = $_SERVER['REQUEST_METHOD'];
if ( $request_method == 'POST' ){
      echo "This is working!!";
      echo $_GET['phone_num'];
}else {
      echo json_encode('No defined function for this method');
}

Tried:

Result: I get This is working!!, but no phone_num which I passed through this code via AJAX

Observation: There is a catch, if we use $_POST['phone_num'] in place of $_GET['phone_num'], then the POSTMAN also doesn’t return any data except This is working!!.

Screenshot:

  • POSTMAN:

Postman Screenshot

  • HTML PAGE:

HTML page Screenshot

Please help where I am going wrong. That’d be great. Thanks.

Comments

Comment posted by RiggsFolly

Small Point

Comment posted by Alok

To check, if things working fine or not. It won’t make a difference, right?

Comment posted by Baracuda078

You send “phone_num” trough a POST request, in PHP you can fetch that variable then with $_POST[“phone_num”] instead of $_GET, with the $_get you can read variables from the url

Comment posted by RiggsFolly

No wont fix it, but a waste of cpu cycles and memory 🙂

Comment posted by RiggsFolly

echo $_POST['phone_num'];

Comment posted by Alok

By this I get

Comment posted by Nijeesh Joshy

i have edited the answer for decoding the json

Comment posted by Alok

One question, why this doesn’t work in POSTMAN? It doesn’t show up any output, it worked on HTML now. I will mark your answer, just wanted to ask this question.

Comment posted by fetch

@Alok i havnt worked in PHP in long while, i think this is some sort of issue with formatting on the JS side. Working with base

Comment posted by Alok

Thanks Nijeesh, your answer helped me. Thank you so much. I hope people will get benefited with your answer, if that will open again. Hahaha!

Comment posted by Alok

Not working Nikhil. Postman works fine, but for HTML, Same output

Comment posted by Alok

It doesn’t work here. I have mentioned in my CATCH description in my question

Comment posted by github.com/fahadpatel/ajax_form

github.com/fahadpatel/ajax_form

Comment posted by Baracuda078

@fahad patel Your using JQuery in your code but not loading it in! So this will not work for him, and we are not sure he whant’s to use JQuery

Comment posted by fahad patel

it is obvoius he will linked it.

Comment posted by Baracuda078

@fahad patel You never know here 😉

Comment posted by Alok

I have already told you that it doesn’t work for

By