Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 10 of 10
  1. #1
    New to the CF scene
    Join Date
    Jul 2012
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    $_POST for arrays or any other method. Help needed!!

    Hi all,

    I am developing an application where forms are generated based on the administrator setup i.e. the admin would setup that question 1 is 'First name', it is a textbox, with max length of 20 and other validations and this question will appear on the user end as a form.

    I am simply using the question numbers as names of the input items which makes it very easy to map to the DB.

    I have created an option which generates more than 1 input item (From Date, To Date and Description). The admin would setup the answer type as 'range' and then input how many times should the 'range' answer type be repeated. This would print the From Date textbox, To date textbox and the Description textarea and repeat it as per the admin setup.

    Now the problem:
    - I am unsure of the naming convention that I should use. I tried f[question number][repeat index], but couldn't use $_POST on a two dimensional array. Could someone guide me as to how this is done. I have done this with single array for checkboxes, but this is a bit tricky.

    - Is there a better method of naming these input items?

    Thanks in advance.

  • #2
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Naming is always up to you IMO... whatever makes your live easiest and easy to read. That being said, always make sure its one word, using underscore or camel case to distinguish words.

    As for multidimensional arrays with post, they work fine. Can you show your attempt to it? It works the same as creating a single dimensional array in practice.

  • #3
    Senior Coder
    Join Date
    Feb 2011
    Location
    Your Monitor
    Posts
    4,327
    Thanks
    60
    Thanked 525 Times in 512 Posts
    Blog Entries
    4
    AFAIK you can't transmit a multidimensional array via post or get. I may be wrong, I had many unsuccessful attempts when creating my generic form poster moons ago.
    See my new CodingForums Blog: http://www.codingforums.com/blogs/tangoforce/

    Many useful explanations and tips including: Cannot modify headers - already sent, The IE if (isset($_POST['submit'])) bug explained, unexpected T_CONSTANT_ENCAPSED_STRING, debugging tips and much more!

  • #4
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,992
    Thanks
    15
    Thanked 233 Times in 233 Posts
    You don't need to use a two dimensional array, per se, you just need to arrange in an 'n X m' manner with the proper text, to do that you use a 'modulus' % conditional statement when building the form, so that there is a line break, or the start of a new tr if you use a table. You can use the % statement at the $_POST end to recover the order of that data too. If you get the data from the inputs via javascript and post the javascript array via a hidden form element, it will appear in the $_POST statement as a comma separated string, you can use explode() to convert the data to an array, numbers may be seen as strings so you may need to specify the data type. Of course you have to avoid commas in the text in that case. What you want to do is entirely possible, it just takes a bit of coding and clear thinking about how to achieve it.

    You could start with a simple form with text inputs, all the inputs that are given data would be used for the headings of a table FI , and the number of inputs with data would determine the width of the table. A special input with a separate name would determine the height of the table , or those numbers could be used to just make rows of textareas, not even in a table. Well, you should pretty much have the idea by now.

  • #5
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Hm... I'm pretty sure I've done multidimensional arrays in the past... its possibly hit or miss, but its also possible I thought I did but came up with another technique for it. I'll review old code when I get home tonight.

  • #6
    Senior Coder
    Join Date
    Sep 2010
    Posts
    1,992
    Thanks
    15
    Thanked 233 Times in 233 Posts
    Quote Originally Posted by Keleth View Post
    Hm... I'm pretty sure I've done multidimensional arrays in the past... its possibly hit or miss, but its also possible I thought I did but came up with another technique for it. I'll review old code when I get home tonight.
    I'm thinking that you can, but the only drawback is that you have to index the form elements when you build the form,
    name="mydata[1][3]", whereas with a one dimensional array they can be name="mydata[]". However you may only have to index one layer, the top one. But I haven't tried it, I just may. Maybe you don't even have to index them, it depends on the way the code is written.

  • #7
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    I wrote this for something this morning, maybe you can adapt it

    PHP Code:
    foreach ($_REQUEST['MenuID'] as $key => $value) {


    and in your form have all the field names as juts

    PHP Code:
    <input type="text" name="MenuID[]" 

  • #8
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    Don't use REQUEST! Always either POST or GET... know where you're getting your variables from!

  • #9
    Regular Coder Arcticwarrio's Avatar
    Join Date
    May 2012
    Location
    UK
    Posts
    721
    Thanks
    20
    Thanked 84 Times in 84 Posts
    i use both get and post in the same script

  • #10
    Senior Coder
    Join Date
    Jun 2008
    Location
    New Jersey
    Posts
    2,536
    Thanks
    45
    Thanked 259 Times in 256 Posts
    To set the same variable? You should always know where a variable is coming from.

    If you set $userID via POST but use _REQUEST, and someone goes to the url with ?userID=1, you can't be sure of which userID you'll get. Its a security hole you can't afford and shouldn't. If you are setting the same variable from either POST or GET for some reason, set it explicitly:

    PHP Code:
    $userID intval(isset($_POST['userID'])?$_POST['userID']:$_GET['userID'])); 
    This way you set the priority order, and you can include other variables to ensure the data is of the form you expect (add an extra hash or something to verify data).

    Using REQUEST is not safe by any means.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •