1

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;
}
?>
14
  • 1
    Why are you comparing to null to begin with? You should be using isset or empty to check if an external parameter is set/empty. Commented Nov 9, 2021 at 8:59
  • What is returned by the AJAX request? Have you checked the console to ensure there's no errors? Commented Nov 9, 2021 at 9:01
  • 2
    Two parts to debug. And it might make sense to manually enter the expected URL in the browser to see the PHP results. Also error_reporting. Commented Nov 9, 2021 at 9:02
  • 1
    Also, you can not properly output an array in PHP using echo, that will only get the you the literal word Array as result. Commented Nov 9, 2021 at 9:02
  • 1
    if you send an ajax request to the same page there are things that you need to do to prevent the entire page source becoming part of the response data Commented Nov 9, 2021 at 9:14

3 Answers 3

1

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);
    } 
});
}
Sign up to request clarification or add additional context in comments.

Comments

0

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

1 Comment

I called postcars. Its my fault tho, I didnt make that clear when I first posted the Q.
0

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>

1 Comment

thank you for good programming practices. //oops, forgot to reply to you ;)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.