Results 1 to 3 of 3
  1. #1
    Join Date
    Jul 2006
    Posts
    56

    Unanswered: Form Inputs with Same Name - Beahvior ?

    Hi, everyone! Lil' help, please.

    I'm migrating from ASP to PHP, and a lot of my work with forms in ASP was based on the behavior of input fields that have the same name. When that happens, the field value is concatenated with commas. For example:

    If you have these lines in a form

    HTML Code:
    <input type="text" name="phone_number" value="555-1234" />
    <input type="text" name="phone_number" value="555-6789" />
    and you Request those fields with something like:
    Code:
    var phone_number = Request.Form("phone_number")
    you'll get a string like this as result: phone_number = "555-1234,555-6789". I could then split it into an array and use the array to insert as many phone numbers as needed. (There's also lots of JavaScript, one of these scripts lets the user add more form elements if needed).

    Now what I noticed in PHP is that if there are two or more input fields with the same name, the next will overwrite the prior, or the value will always be the last one (not too sure). Is that the expected behavior? Any way to change it or to work around it?



    Edited: 11/23/2007
    &#168;&#168;&#168;&#168;&#168;&#168;&#168;&#168;&# 168;&#168;&#168;&#168;&#168;&#168;&#168;&#168;
    I just wanted to add that I know about using brackets in the form element name. It's just that using brackets there is gonna cause me so much trouble, I really didn't want to handle it like that. What I meant to ask is, is there another way ?



    Thanks for any help.
    Last edited by igordonin; 11-23-07 at 16:02.

  2. #2
    Join Date
    Apr 2006
    Location
    Denver, Co. USA
    Posts
    242
    PHP requires an array. I don't know why php cannot access the coma-concatenated data, because that is produced by the browser no matter what type of form processing code there is.

    If you want a same-name variable, you will need to use one of the following methods:

    Auto-indexed -
    HTML Code:
    name="phone_number[]"
    or specifically indexed -
    HTML Code:
    name="phone_number[0]"
    name="phone_number[1]"
    or unique names -
    HTML Code:
    name="phone_number1"
    name="phone_number2"
    Why is using an array a problem. I have created elements using javascript for all the above methods, but by using the array, php is able to loop over the elements, while using unique names, it requires more php code to access the results.

  3. #3
    Join Date
    Jul 2006
    Posts
    56
    Hi, dbmab! Thanks for the help.

    PHP requires an array. I don't know why php cannot access the coma-concatenated data, because that is produced by the browser no matter what type of form processing code there is.
    I suppose you're right, but that's not what I could make out of the pages I've researched online. Looks like I'm just gonna have to use the brackets.

    My problem with the brackets is while referencing to some form element by name later on. I have many lines like this:
    Code:
    this.form.field_name[0].value
    and I certainly may not do this:
    Code:
    this.form.field_name[][0].value
    Ok ... I may use elements[field_name] or elements[numeric_reference] like that below:
    Code:
    this.form.elements['field_name[]'][0].value
    but that means I'm gonna have to change too many scripts.

    I was hoping I wouldn't have to. I'm moving forward, though.

    Thanks again for the help.

Posting Permissions

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