<input type="text" name="child_name[]" />
<input type="text" name="child_name[]" />
When you submit that, $_POST['child_name'] will be an array of 2 elements.
Thanks for your responses. I’m not sure what you are trying to show me with your code, @Mittineague. I already have my form set up and processed so that the contents are inserted into my database - except for the values from the form elements dynamically created by JavaScript.
@rpkamp, I know how to set up form inputs to get arrays. I do that quite often when I use the PHP framework that I built.
What I am trying to do is bridge the gap between JavaScript and PHP. In my JS file I have gotten as far as dynamically creating the form elements and gathering their input values into two arrays in my script.js. I now need to somehow get those arrays to my PHP controller as PHP arrays for insertion into my database along with the rest of the form contents.
That’s why I’m using JSON. But if there is another way, that’s fine too.
Again, I’m asking for the steps involved to bridge this gap, not the code. At this stage I’d like to try it for myself first, then ask questions if I get stuck. I either don’t understand the dynamics or I’m missing something obvious.
As an alternative to sending the arrays encoded as form values, as ScallioXTX suggests, you can also send the JSON as a POST request with the content-type application/json. When you send JSON to PHP in this way, it won’t be available via the $_POST superglobal. Instead you have to read from the request body and run the resulting string through json_decode().
WebMachine:
What I am trying to do is bridge the gap between JavaScript and PHP. In my JS file I have gotten as far as dynamically creating the form elements and gathering their input values into two arrays in my script.js.
As @rpkamp says, those arrays are not necessary. You can send the entire form as a FormData object, and it will be available to the PHP script like a regular form submission. Here’s a link (with a spoiler alert):
Is that an official standard? From what I’ve read there was a W3C draft for this but it got cancelled.
https://www.w3.org/TR/html-json-forms/
Even if it’s not an official standard, its basic functionality has been adopted by every major browser, so they at least acknowledge its existance.
However, OP specifically states that he’s trying to transmit JSON he’s already created to PHP to use json_decode on them.
If he’s already got them in JSON, the JS JSON library has the command JSON.stringify().
At that point, all of the arrays are strings, ready to be decoded by PHP.
You can submit any number of strings as you want with a POST set.
If you actually wanted to, you could put your multiple JSON arrays into a singular JSON object, and stringify that and send it.
rpkamp:
Is that an official standard? From what I’ve read there was a W3C draft for this but it got cancelled.
https://www.w3.org/TR/html-json-forms/
Oh that’s interesting, I wasn’t aware of that. Looks like that would have allowed the browser to directly submit forms as JSON?
Simply that all POST values are strings with the hope that it would clue you in to thinking of stringifying the JSON.
I did not know about the possibility of sending JSON as JSON. That would simplify the stringify - json_decode steps.
I used stringify in my JavaScript already. I need to figure out how to get the JSON arrays from my javascript file, script.js (they are called jsonIngredients and jsonDirections) into my PHP controller file, Recipes.php, so that I can use json_decode() to convert them into PHP arrays. After that, I’m good to go. (Or try one of the other solutions mentioned here).
Okay, then the bit about it being in a file is moot, because you’ve read that into memory. You’ve stringified your values.
The question becomes “Do you want the processing to be visible.” IE: Do you WANT click a button, have the page reload pointing to myprocessorscript.php , or do you want to do it with AJAX in the background without leaving the page.