I know you can serialize an array to allow you to post an array but I've heard that theres a way of doing it so that it passes as an array. My problem is that I want to send a different number of instructions through the post each time so it looks something like <textarea name='instruction[]'></textarea> but I'm not sure how to assign the posted value of instruction[] at the other end. Am I doing something unneccessary and there's an easier way to do this?
I've found a little website with a description on this at http://www.phpnoise.com/tutorials/6/4 Just going to try it out now. Edit : I just give myself a heap of problems!! I was actually wanting to send 2 arrays so I cant just copy and paste the code lol
This is what I've done: HTML: <textarea name="instructions"></textarea> Hit enter after every instruction PHP: $arrInstructions = explode("\n", $_POST["instructions"]); Then you can access each line of the instructions as an array element, and loop through using: foreach ( $arrInstructions as $instruction ) { // Do code per instruction }
No offense, but that seems like more of a hack than a solution. It may be plenty good to accomplish what you are trying to do. I would think parsing your data into an xml string would be a more complete solution - <instructionset> <instruction id="1">one instruction</instruction> <instruction id="2">another instruction</instruction> </instructionset> Code (markup): And then use something like XML simple to parse the data server side. Obviously these are more complex solutions to a simple problem, but I think it would end up making things easier on you in the long run if this is an application you see growing a good deal beyond where you are at today. Alternatively, you could use javascript to create new form elements for you following a numbered naming convention and parse the data as POST elements.
Thanks nevetS. I'm not too familiar with XML yet as I've only spent a few months learning it but I'll certainly try and implement your solution at some point just to teach myself how to use XML. It's for a project that shouldn't be too huge and the function will only be used admin side so performance isn't vital. I do however want to learn the best practises so I will definitely follow your advice up at some stage. Cheers again!
What do you mean? I used the example given on http://www.phpnoise.com/tutorials/6/4 for the solution.
I agree with JohnT, what is wrong with a simple foreach() loop? The XML solution is way over engineered for this case, IMHO. Why go to all that extra trouble when it is simply not necessary? The instruction[] array is what I'd use. Depending on what kind of instructions you're sending, maybe even an array isn't necessary. Bash commands can just be semi-colon separated. Or you could type one command to a line in a text box and loop through them by creating an array at the other end with split() ... there are lots of simple ways this could be done.
Remember, its a differing number in the array each time though which was my original problem. If it had been for a site where I expected thousands of visitors a day using the function then I would definitely look for the most efficient way of executing the form but for this particular project, a quick hack was all that was needed lol.
That's the point of foreach() - you don't need to know how many elements are going to be in there, it will loop through all of them.
Thats what I used I have it all working now but the debate is now what is the best method to use. Sorry for that confusion there.
Well, to defend my original suggestion... XML is a good way to go looking to the future if one of the following is true: 1) instruction set will get complex in the future. 2) If you want people to build third party tools, and you anticipate changes to your original structure. 3) If you want to support web services in the future. 4) Your instruction set contains any one to many relationships. 5) You want to eventually document a standard for your application. I'm a little fuzzy on the purpose here, but certainly one of those may be true. People run away from XML because it seems hard. Once you spend a little time with it, it's as easy as pie and it makes a lot more sense most of the time because it's structured data. I would never send a set of executable instructions as a POST - it's a pretty wide open security problem. Instead, you should send instructions that are to be validated and interpreted by your server side script. (i.e. verify they have valid lengths, appropriate properties, no shell escapes, etc.) Anyhow, just my 2 cents
Ahhh I think you are getting a little mixed up about what I'm sending. The instructions variable aren't executable. They are just fields containing text. I've got a database with a load of tutorial type things and instructions is the field on how to do each step. Does this clear things up?
So as I see it, you have an admin tool that you want to have someone type in these step by step instructions for putting into your tutorials, right? And to make it simple you want them to be able to use a textarea? So going back to my post earlier you can just do this in HTML: <textarea name="instructions"></textarea> Code (markup): give some instructions to them on the page that they need to seperate steps by pressing the enter key. Then in your PHP: $instructions = explode("\n", $_POST["instructions"]); Code (markup): Remember you do not need []'s in the HTML since you are creating an array with the explode function. Then each line from the textarea will be a new element in the array, and you may loop through that using your foreach. BTW: XML is a great format for serializing your data. I use it on a number of very huge sites, parsing the XML out using XSLT stylesheets.