Search code examples
javascriptphparraysget

Javascript array is not completely assigned to php array


Below is my array contents in javascript. I'm passing it using get call to a php page. I am also passing other array's along with this array. Those array values are passed completely. Only this array is passing partial values. I also alerted it in javascript and its complete there. Whereas in php its taking only 2 values instead of 14.

var testPlanNameArray= ["Eligibility-Real Time Eligibility Cascading Validation", "Eligibility-SubmitterRouting", "Eligibility alias for defth", "Eligibility-Submitter x", "Eligibility-SubmitterRouting", "Eligibility-SubmitterRouting & Partner alias", "Eligibility-SubmitterRouting", "Eligibility-SubmitterRouting & Partne", "Eligibility-SubmitterRouting & Partner", "Eligibility-SubmitterRouting ", "Custom Maps Validation", "Custom Maps Validation", "Eligibility-Real Time Eligibility Custom Maps Validation", "Eligibility-Real Time Eligibility Custom Maps Validation"];

I'm passing using below code to php

$.get("dbValidation.php?&testIDArray=" + testIDArray + "&testPlanNameArray=" + testPlanNameArray + "&errorArray=" + errorArray + "&historyErrorDescArray=" + historyErrorDescArray  + "&expectedDEPProp=" + expectedDEPProp  + "&actualDEPProp=" + actualDEPProp).done(function(data3) {
                                // alert(testPlanNameArray);
                                        console.log(data3);
                                        console.log(testPlanNameArray);
                            });

In php page Iam just doing

<?php
$testPlanNameArray = explode(",", $_GET['testPlanNameArray']);
print_r($testPlanNameArray);
?>

When I do console.log(data3) it will print

Array
(
    [0] => Eligibility-Real Time Eligibility Cascading Validation
    [1] => Eligibility-SubmitterRouting 
)

I tried $testPlanNameArray = json_decode($_GET['testPlanNameArray']); but still it is not taking complete value. The problem is only with respect to this array. For rest array it is working fine.


Solution

  • There may be an encoding issue. I suggest you pass an object to $.get, and it will encode it properly.

    $.get("dbValidation.php", {
        testIDArray: testIDArray,
        testPlanNameArray: testPlanNameArray,
        errorArray: errorArray,
        historyErrorDescArray: historyErrorDescArray,
        expectedDEPProp: expectedDEPProp,
        actualDEPProp: actualDEPProp
    }).done(...)
    

    This encoding will actually create arrays in the $_GET parameters, so you don't need to use explode() or json_decode() at all.

    Note that there's generally a limit to the length of URL parameters, typically 1024 or 2048. So trying to pass long arrays on the URL may not work well. You should use $.post, which has much higher limits.