I am working on trying to increase my JavaScript skills beyond the beginner level, and decided to try to figure out how to add a feature to a recipe application that I built using PHP/mySQL. It involves dynamically adding input boxes for ingredients and textareas for directions to the Add Recipe form, the contents of which are to be processed along with the rest of the form submission. The number of ingredients and directions are pulled from two input boxes earlier in the form.
This is what I have managed to piece together in a separate JS file:
I can read the number of ingredients and directions inputted and use that to dynamically add the correct number of form elements.
I can get the input values and create a JS array of the ingredients, and a JS array of the directions.
I can convert the arrays to JSON.
And this is where I am stuck. I now need to somehow get that JSON from my JS to my PHP controller to use with the PHP json_decode() function, so that I get two PHP arrays that I can process in my controller along with the rest of the form inputs in my $_POST array when I submit the form.
I am looking for a strategy or set of steps that will take me from the JS to the PHP, not the actual code at this time. I would like to try to figure that out for myself if possible. I tried Googling this situation, but all provided solutions have only served to confuse. Once I have an understandable set of steps, if I get stuck I will be back here with my code for more help.
This is a learning exercise - please don’t just hand me the code yet. Thank you.
There might be limitations depending on the size of the array. I can think of two approaches, one very much simpler as long as any size limits allow it.
The $_POST values are either integers or strings. The JSON arrays have elements which are strings. I guess the longest would be the directions (some recipes might have upwards of 10 directions, each one containing multiple sentences). What kind of size limits are we talking about?
If I can get the JS JSON converted into a PHP array and into my controller method that processes the form, it does not need to be part of the $_POST array. I can process both in my controller to send off to the database. I would just like to send it to the controller at the same time as I send the $_POST array.
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().
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):
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.
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).
On the client. Nothing has gone to the server, and won’t until I get these arrays into my PHP controller and along with the $_POST array, process the form values so I can insert them into my database.
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.