Home > Mobile >  Why does PHP file return "array", but not its contents
Why does PHP file return "array", but not its contents

Time:11-09

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;
}
?>

CodePudding user response:

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);
    } 
});
}

CodePudding user response:

You did not call the function postcars();

The JavaScript file should be like this:

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

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

postcars();

CodePudding user response:

What I'd normally do would be to test if a particular parameter is in the request ( using isset() or !empty() ) and then call ob_clean() to discard any possible output buffer generated at that stage. Process the request and then terminate the processing completely as it is a different thread. The response then that is set back is only what you want to send. This approach with a GET request would cause a regular page loading where there is a querystring, such as forms1.php?result=banana, to halt at that stage and display no further content however/.

<?php
    if( isset( $_GET['result'] ) ){
        ob_clean();
        $filename = $_GET["result"];
        /* do interesting stuff with variable... */
        
        exit('Hello World! - Send a response');
    }
?>
<!DOCTYPE html>
<html lang='en'>
    <head>
        <meta charset='utf-8' />
        <title>geronimo</title>
        <script src='//code.jquery.com/jquery-latest.js'></script>
    </head>
    <body>
        
        <script>
            var nums = [1,2,3,4];

            function postcars() {
                $.ajax({
                    type    : 'GET',
                    url     : 'forms1.php',
                    data    : {"result": nums},
                    success: function(resp){
                        alert(resp);
                    } 
                });
            };
            // oops, forgot to call the ajax function
            postcars(); 
        </script>
    </body>
</html>
  • Related