Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

PHP not retrieving POST variable data

I can’t figure out why my php page cannot get the data from the array posted to it.

The data’s definitely there in the array in Javascript before it’s posted as it’s displayed in the alert, but when I try to output the array in php after it’s been posted, I get

Undefined index: oaIDArray in D:\wamp64\www\Stelram\bomTerminals\testphp.php on line 10

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

This is the php:

<?php
session_start();

require '../db_conn.php';

$conn = sqlsrv_connect($serverName, $connectionInfo);

$oaIDArray = json_decode($_POST['oaArrayStr']);

$oaCount = count($oaIDArray);

for ($i = 0; $i < $oaCount; $i++) {
    print_r($oaIDArray[$i]);
}
?>

This is the Javascript:

$(function checkItems() {
    //Assign Click event to Button.
    $("#btnGet").click(function () {
        const oaArray = [];
        let count = 0;
        $("#myTable2 input[id = 'releasedCheck']:checked").each(function () {
            var row = $(this).closest("tr")[0];
            let oaID = row.cells[10].innerHTML;
            oaArray[count] = oaID;
            count++;
        });
        var oaArrayStr = JSON.stringify(oaArray);
        
        alert(oaArrayStr);
        
            $.ajax
            ({
                url:'../bomTerminals/testphp.php',
                type: 'POST',
                dataType: 'json',
                data: {oaArrayStr: oaArrayStr}
            })
        return false;
    });
});

TIA

>Solution :

You are making two HTTP requests.

Ajax

The first request you make is the POST request initiated by the call to $.ajax.

This has the request body and you don’t get the error message

New tab

The second request you make is a GET request initiated by picking the Open In New Tab option from the menu in the Network section of the browser’s developer tools.

This is a different request.

It is not a POST request.

It does not have the data in it.

Therefore you get the error.


If you want to see the response to the Ajax request using the Network tab, you need to look at the Response section of the Network tab instead of opening the URL in a new browser tab.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading