Skip to content
Advertisement

Why does PHP file return “array”, but not its contents

I am tring to send an array to PHP, PHP however, only outputs “array”. I have two files: forms1.php and forms1.js. The PHP if statement never turns true. I have tried without the if statement too.

Thanks for pointing it out in the comments, Yes I call the function, and the alert returns entire forms1.php code.

old title: Why does my php file not see GET[“result”]? // this title is wrong

forms1.js

var nums = [1,2,3,4];

function postcars() {
    $.ajax({
    type    : 'GET',
    url     : 'forms1.php',
    data    : {"result": nums},
    success: function(resp)
    {
        alert(resp);
    } 
});
}

forms1.php

<?php
if ($_GET["result"] != null){
  $filename = $_GET["result"];
  echo $filename;
}
?>

Advertisement

Answer

nums is an array, elements inside might be sent as individual values and the parameter name is result[], make it a single string by using JSON.stringify

var nums = [1,2,3,4];

function postcars() {
    $.ajax({
    type    : 'GET',
    url     : 'forms1.php',
    data    : {"result": JSON.stringify(nums)},
    success: function(resp)
    {
        alert(resp);
    } 
});
}
User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement