0

I am trying to send a javascript array to an external php page, but the only thing the php page is picking up is the fact that I am sending array, not the actual data within the array.

javascript -

var newArray = [1, 2, 3, 4, 5]; 

        $.ajax({
            type: 'POST',
            url: 'array.php', 
            data: {'something': newArray}, 
            success: function(){
                alert("sent");
            }
        });         

External PHP Page -

<?php 
    echo($_POST['something'])); 
?>

I know this question has been asked before, but for some reason, this isn't working for me. I have spent the last couple days trying to figure this out as well. Can someone please point me in the right direction.

current output (from php page) - Array (thats all the page outputs)

3 Answers 3

3

You should use var_dump in stead of echo.

Echo is only for strings, integers, floats and it will print 1 if a boolean is TRUE, and Array for an array.

Sign up to request clarification or add additional context in comments.

6 Comments

thanks, i just changed that, I am now getting that 'something' is an undefined index, and the var_dump is resulting in NULL
If you were getting a readout of Array when you echoed $_POST['something'] it means you had an array there not a NULL. You changed something.
you're right, i reverted back to past code and am now getting "array(0) { }"...??
Take the entire JavaScript array and put it in a text file see how much space that would take up. Next multiply the length of the key 'something' with the number of elements in the array 2388. Add that to the number of bytes the text file is occupying. And come back with the final value and tell us (you might be exceeding post_max_size) which would be 2M by default but may be 4 or 8 on your server. Also check what your current post max size is.
thanks for the advice so far, i really appreciate it, but would you happen to know of an efficient way to get my javascript array to a text file. Right now, I am using "console.log" to see my array and copying that to a text file, but it seems as though it will take some time with the amount of data that i have
|
1

You could also use print_r() which is a little more readable.

1 Comment

Readability comes after. If you want readability install xdebug which prettyprints stack traces and makes var_dump look readable.
-2

Y don't you convert array into string with some special character as separator and explode it into php?

1 Comment

the real array i need this to work for has a size of 2388. Would this method work for an array of that size?

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.