r/PHPhelp • • May 31 '23

Javascript Array Is Getting Converted To A String in PHP File?

I have a Javascript file that is processing a form. It first takes all the input from the form and makes an associative array of key value pairs. I can confirm that at this stage, it is regsitering as an array in JS (tested it with the isArray function).

I then make a formData variable and append the array to it (using a new key name to refer to it). And then use XMLHttpRequest to post it to a php handler.

 let xhr = new XMLHttpRequest();
 xhr.open("POST", filePath) 
 let formData = new FormData();
 formData.append("processedArr", associativeArr);
 xhr.send(formData);

But when I check the variable type in my PHP file with gettype, it is showing up as a single string. All of the key value pairs are just chained together in one single string. What are some reasons why this happens? How come its getting converted from an array to a string?

8 Upvotes

12 comments sorted by

6

u/ZippyTheWonderSnail May 31 '23

"The field's value is the second argument to append. This can be a string or Blob (including subclasses such as File). If none of these are specified the value is converted to a string."

https://developer.mozilla.org/en-US/docs/Web/API/FormData/append

3

u/SubzeroCola May 31 '23

Wait so you can't define your own key value using the append function? What I have in my JS file is already an associative array. I just want to pass this onto my PHP file..........and I was using the append function just so that I can give that associative array an identifier.......so that when I grab it from the $_POST array, I can refer to it by that identifier.

10

u/Plastonick May 31 '23

It's important to remember that PHP isn't Javascript, and that means they don't necessarily know how to talk to each other. We need to help them.

In this case, it's not merely javascript trying to pass information (your associative array) to PHP, it's having to do that across another layer which is HTTP.

Essentially, you're asking Javascript to pass an abstract piece of data (array) through HTTP and have that information make sense and become a PHP associative array that you can use at the other end.

Unfortunately, that's not possible without some way of translating that data so all of the different layers understand what's happening.

Traditionally we use some sort of data-transfer language (think of things like CSV, XML, JSON) which can represent abstract information in a fairly universal format (a string).

I think you've already got your answer elsewhere, this is just a brief way to explain why those answers are necessary.

2

u/ZippyTheWonderSnail May 31 '23

new FormData(form)

"An HTML <form> element — when specified, the FormData object will be populated with the form's current keys/values using the name property of each element for the keys and their submitted value for the values. It will also encode file input content."

https://developer.mozilla.org/en-US/docs/Web/API/FormData/FormData

1

u/ardicli2000 May 31 '23

Once you append whole array into a formData key, the whole array will be one value hence will be stringified at best .

Rather you should tun a foreach on your array to push vales as formData.append(key, value)

2

u/bobd60067 May 31 '23

TLDR; use json_decode()

Long explanation... On the client side (ie, In the browser), you have a js array. In order to pass it to the server via a form, it has to be converted to a string. Actually, the string is an encoding that captures the array's structure and values. On the server side (in your case, in your php script), you have to decipher the string to recreate the array. In php, you use json_decode().

2

u/SubzeroCola May 31 '23

Cool that makes sense now! So would I have to first encode it as JSON in my javascript file before passing it to the PHP file?

2

u/cursingcucumber May 31 '23

Well this is just one way to do it. Communication with the backend (e.g. PHP) is often done using JSON. The frontend encodes the data into JSON, sends a request with the appropriate content-type header to the backend, which in turn checks that header and decodes the content accordingly.

Another way (your attempted way) is to send form data directly. This is also sent as a string but encoded in a different way and most importantly, sent with the correct content-type (application/x-www-form-urlencoded).

Some examples can be seen here: https://developer.mozilla.org/en-US/docs/Web/HTTP/Methods/POST

1

u/Plastonick May 31 '23

Yes, you can use JSON.stringify(<assocArray>) client-side.

1

u/tridd3r May 31 '23

I make the associativeArr a json string and then json_decode at the other end (also I prefer fetch):

Javascript:
 let xhr = new XMLHttpRequest();
 xhr.open("POST", filePath) 
 let formData = new FormData();
 formData.append("processedArr", JSON.stringify(associativeArr));
 xhr.send(formData);

then in your php:
$processedArr = json_decode($_POST['processedArr'], true);

2

u/Audience-Electrical May 31 '23

You posted the legit solution first and still got thumbed down!

Here, take my wife

2

u/tridd3r Jun 01 '23

I'm here to learn just as much as the next person, if I'm getting downvotes it would be nice to know why...

the wonderfulsnail technically answered the question, but I thought i provided a reasonably solution to the implied question