r/PHPhelp • u/SubzeroCola • 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?
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
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
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