Results 1 to 11 of 11

Thread: Form elements of the same name

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    Form elements of the same name

    In ASP if I have a form with elements of the same name I get the posted information for that element as

    data1,data2,data3,data4

    in PHP I only get Data1 or one value returned

    I worked around by renaming the elements differently but is not really the solution that provides me with flexible code.

    Does php return same named elements in one shot or just one element?
    I'm testing on Windows 2000 with Apache, php4

  2. #2
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    what is the code?

    you can only get them the same if they were an array.

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    From elements one more time....

    //Simplified Form example
    <form action>
    <input type=text name=item value=1>
    <input type=text name=item value=2>
    <input type=text name=item value=3>
    <input type=text name=item value=4>
    </form>

    In ASP I can go Items=request.form("item")

    and Items will contain "1,2,3,4"

    in PHP I only get a single value returned. I can't remember it is either "1" or "4" but not all.

    I've tried to see if the data was in an array as in
    below, but no luck.
    echo $item(0)
    echo $item(1)
    echo $item(3)
    echo $item(4)

  4. #4
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337
    you have to add them as array.

    <input type=text name=item[] value=1>
    <input type=text name=item[] value=2>
    <input type=text name=item[] value=3>
    <input type=text name=item[] value=4>

    that is proper coding even for ASP. if it even work sin ASP. :P

    so then you get them like this

    echo $item[0];
    echo $item[1];
    echo $item[3];
    echo $item[4];

    doesn't make any since it you name then all the same value without making them an array.

  5. #5

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    hmmmm

    Well as far as making sense I'm not the brains behind how this stuff works, all I know is that ASP returns same named elements in one long string seperated by a comma and PHP does not.
    it actually is quite nice to just have a single named element as opposed to item(0), item(1) etc.
    If I have have to later add another option to asp I just paste the element (in this case a checkbox) in the HTML form and I'm done, my asp code takes care of the parsing. IN PHP I have to go in a modify my code to know a new element index has been added...
    Now what makes more sense?

  6. #6
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    PHP makes more sense. If I'm following what you're saying.

    I know that if all my form values were returned in the same variable, seperated by commas, I'd hate it. How do you get out a single value? Would you have to split it into an array? Wouldn't it make sense to already have it in an array?

    What if one of the values had a comma? What does it do then?

    Or am I just not understanding anything?
    My evil laugh has a squeak in it.

    kristopherwilson.com

  7. #7
    Frenzied Member
    Join Date
    Nov 1999
    Posts
    1,337

    Re: hmmmm

    Originally posted by Incognito44

    Now what makes more sense?
    php does, that way you know what is going on. ASP sounds stupid if you had to do that. and like Hobo said, what if the contents was a comma or some other character that blows that code out of the water?

    in php you know exactly what you want and what should be there, in ASP it doesn't sound like you have that much control. maybe it is just me though.

  8. #8
    Addicted Member Phenix's Avatar
    Join Date
    Sep 2002
    Location
    Near A Cube
    Posts
    228

    Thumbs up HTML Form Processing

    I came across this same "anomaly" when updating an HTML form that was handled by a Perl script. It's valid in HTML to give elements of the array the same name on the HTML side (the same way your ASP script handled it).

    But PHP wants an explicit [] for each array element on the HTML side. Personally I think the PHP way is more intuitive. Unfortunately, if you change the form processing script(ing language), you have to know how the form was written.

  9. #9

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    Thanks

    Thanks Phenix,
    That answered my question.

    For those who want to know why it makes more sense to me "in this particular situation"
    The form elements were checkboxes (sorry, no code blowing there--it's on or off, data or no data)
    Since I already wrote a function to parse the commas adding a new checkbox to the group was as simple as adding it to the form....done
    In php I have to update the code and the html-
    Simpler and less work makes more sense to me...I'm open to alternate solution though....G

  10. #10
    Stuck in the 80s The Hobo's Avatar
    Join Date
    Jul 2001
    Location
    Michigan
    Posts
    7,256
    Why would you have to update any code in PHP?

    Code:
    for ($i = 0; $i < count($_POST['chk_values']); $i++) {
        //do something with $_POST['chk_values'][$i]
    }
    You can add as many checkboxes as you want and the loop will always work.
    My evil laugh has a squeak in it.

    kristopherwilson.com

  11. #11

    Thread Starter
    Lively Member
    Join Date
    Feb 2002
    Posts
    98

    will use it...

    Being fairly new yto php and a bigi asp user-I'm used to posting single form elements and haven't had to do it as arrays in the past. Thanks for the info...G

Posting Permissions

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



Click Here to Expand Forum to Full Width