0

I have never done anything like this, and I would like to know how to do it. I need to put 4 inputs into a sub array if it is field out

I know that when i $_POST the form to the server it sends the names of the inputs but how do I get the input to be allowed to have the same name

for example

the sub array i need it to be in is offers here is what i dont know. How do i get the following inputs

<input name="offers[]['minspend']" value="15.00"/>
<input name="offers[]['minspend']" value="5.00"/>

<input name="offers[]['minspend']" value="19.00"/>
<input name="offers[]['minspend']" value="8.00"/>

<input name="offers[]['minspend']" value="30.00"/>
<input name="offers[]['minspend']" value="7.00"/>

<input name="offers[]['minspend']" value="100.00"/>
<input name="offers[]['minspend']" value="10.00"/>

is this correct or wrong?

thanks

2
  • Generally there isn't a problem with sending multiple input's with the same name to the server. If you run either firebug or fiddler you can watch the request being sent to the server and see how the inputs get passed. I would just name the inputs "offers" as it should be received by the server as offers[] Commented Sep 13, 2012 at 4:04
  • What does this question have to do with JavaScript? Commented Sep 13, 2012 at 4:24

3 Answers 3

2

It depends a bit on the back end technology that processes your request (java, php, whatnot), but from an html standpoint multiple elements with the same name will just send their value with the same parameter name. You don't need any special [] syntax.

GET /mypage.html?offer=15.00&offer=5.0&offer=19.0 (etc, could be post too)

Most languages that provide built in support for html requests represent this request as a map, with a key named "offer" and a value that is an array or list containing the values submitted.

For example http://docs.oracle.com/javaee/6/api/javax/servlet/ServletRequest.html#getParameterMap()

Sign up to request clarification or add additional context in comments.

6 Comments

From the looks of it ($_POST), the asker is using PHP. In all PHP examples I've ever seen, the special bracket notation [] is always present. I don't know if it's required or not, but my impression is that it is.
I've done very limited amounts of php, but this is an http thing... if such a thing is done in php, it probably is a naming convention. (Not sure though).
the comments in the docs seem to support this notion: note the example where they name something 55 repeatedly... php.net/manual/en/reserved.variables.post.php
That example is a radio input. It only has one value when sent to the server. If multiple values will be sent to the server, I'm pretty sure PHP requires the [] notation in order to signify to PHP to represent it as an array.
ah typical of php, the [] is a shortcut to get Php to do something... php.net/manual/en/reserved.variables.post.php#87650 it causes the array to contain a convenient set of values...
|
1

This form

<input name="offers['minspend'][]" value="15.00"/>
<input name="offers['minspend'][]" value="5.00"/>

<input name="offers['minspend'][]" value="19.00"/>
<input name="offers['minspend'][]" value="8.00"/>

<input name="offers['minspend'][]" value="30.00"/>
<input name="offers['minspend'][]" value="7.00"/>

<input name="offers['minspend'][]" value="100.00"/>
<input name="offers['minspend'][]" value="10.00"/>

on doing var_dump($_POST) [assuming form method=post] give:

array(1) {
    ["offers"] = > array(1) {
        ["\'minspend\'"] = > array(8) {
            [0] = > string(5)"15.00"
            [1] = > string(4)"5.00"
            [2] = > string(5)"19.00"
            [3] = > string(4)"8.00"
            [4] = > string(5)"30.00"
            [5] = > string(4)"7.00"
            [6] = > string(6)"100.00"
            [7] = > string(5)"10.00"
        }
    }
}

So, that is how you do it.

you can remove 's around minspend. they aren't needed.

You were almost there. offers[]['minspend'] means that you get:

array(){
    array(){
        'minspend' => "15.00"
    }
    array(){
        'minspend' => "5.00"
    }
    .. and so on
}

So what is happening is, when you do something like arr[] = 1, 1 is inserted into array arr.

Comments

0

Well first let me tell you what i understood from the question. You want multiple input fields to have same name and then you want to select them all to perform some action. If this is the case i'd like to present a different approach - why don't you assign same class (cssclass) to all the controls you want to have same name. That way you'll be able to select them all together using document.getElementsByClassName("yourclassName"). which will return you an array of all the elements having class attribute as yourclassName. Or if you wanna stick to name then you can use document.getElementsByName("elementName"); which returns you an array of elements having name as elementName. Hope its helpful.

2 Comments

To stay more inline with what they want: could use Name instead of ClassName.
The OP mentioned sending the fields to the server. In this case, name matters, not class.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.