1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

posting an array through a form

Discussion in 'PHP' started by Weirfire, Oct 6, 2005.

  1. #1
    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?
     
    Weirfire, Oct 6, 2005 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    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
     
    Weirfire, Oct 6, 2005 IP
  3. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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
    }
     
    durango, Oct 11, 2005 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    You forgot to put the

    <textarea name='instructions[]'></textarea>
    Code (markup):
    or did you?
     
    Weirfire, Oct 11, 2005 IP
  5. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #5
    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.
     
    nevetS, Oct 11, 2005 IP
    Weirfire likes this.
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    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!
     
    Weirfire, Oct 11, 2005 IP
  7. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Maybe I'm missing something, but can't you just use foreach to step through the array(s) ?
     
    johnt, Oct 12, 2005 IP
  8. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #8
    Weirfire, Oct 12, 2005 IP
  9. michele

    michele Peon

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #9
    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.
     
    michele, Oct 12, 2005 IP
  10. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #10
    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.
     
    Weirfire, Oct 12, 2005 IP
  11. Alx

    Alx Member

    Messages:
    94
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    48
    #11
    use foreach or sizeof($instruction) to get the number of elements in array
     
    Alx, Oct 12, 2005 IP
  12. johnt

    johnt Peon

    Messages:
    178
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    0
    #12
    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.
     
    johnt, Oct 12, 2005 IP
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    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.
     
    Weirfire, Oct 12, 2005 IP
  14. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #14
    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 :)
     
    nevetS, Oct 12, 2005 IP
  15. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #15
    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?
     
    Weirfire, Oct 12, 2005 IP
  16. durango

    durango Guest

    Messages:
    83
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #16
    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.
     
    durango, Oct 12, 2005 IP