isset($_POST[‘value’]) data is not recieved by php

I’m trying to send some values from a javascript file with POST to a php page.

AJAX code:

let inputval = $input.val();
$.ajax({url: "../checkout/test.php", type : 'post', data : {'inputval': inputval}, success: 
function(data){ 
                console.log(data);
              }

PHP code

 if (isset($_POST['inputval'])){
        $result = $_POST['inputval'];
        echo json_encode($result);
    }
    else {
        echo 'Not recieved';
    }

When I check it in networks all is as expected, but when I open the actual page the data is not recieved.

Answer

when I open the actual page the data is not recieved.

First you make a POST request using JavaScript. The data is visible in the network tab. It it sent to the PHP program. The PHP program responds. The response is logged to the console.

Then you “open the actual page”. This is a new request. It is not the POST request with the data. $_POST will be empty this time. The response will be the response to the new request, not the previous POST request. Since the content of the response depends on $_POST and that has changed, this response is different.

If you want to store the inputval data from the first request and then display it in subsequent requests you need to explicitly store it somewhere (such as a database or a session variable) and then check that location for data when you receive the subsequent request.

The logic would run something like:

IF this is a POST request
    store the inputval in the session
    send a confirmation message to the client
ELSE IF the session contains an inputval
    send it to the client
ELSE
    send a message to say that there is no data to the client