#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0

    request problem with array of fields


    Hi there,
    I have a changing number of fields on my form. For php I handle them as an array like this:

    for($i = 0; $i < $whatever; $i++){
    echo "<input type=text name='fields[".$i."]' value='".$fields[$i]."'>\n";
    }

    On the next page I can loop through that array with:

    $count = count($fields);
    for($i = 0; $i < $count; $i++){
    echo $fields[$i]."<BR>\n";
    }


    Is there an equivalent in JSP?

    PS: Yes, I know that usage of globals isnīt correct anymore.
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    12
    This should be equivalent...

    Code:
      for ( int i = 0; i < whatever; i ++ ) {
        out.println("<input type='text' name='fields[i]' value='" + fields[i] + "'>");
      }
    Code:
      int count = fields.length;
      for (int i = 0; i < count; i ++) {
        out.println(fields[i] + "<BR>\n");
      }
  4. #3
  5. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    Thnx, but thatīs not what I meant.

    I have a form on the first page and want to submit it to the 2nd page. Now the form-data should be cached in the request-object, right?

    This object has limited methods to access the form-data, for example "request.getParameterNames" or "request.getAttributeNames" which return an Enumeration.

    My problem is that it doesnīt seem possible to access only certain fields with unknown names - just ALL fields or fields with a known name.

    Is there really no way to do this?
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Jan 2003
    Location
    No es importante
    Posts
    2,065
    Rep Power
    14
    Are you saying that you may not know what fields/values are being passed from one form to the next?
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    4
    Rep Power
    0
    Yes.
    I have a Database with players (5 at the moment, so there is no need to go for performance) and I want to let them select teams via the form.
    May be that all players participiate, may be that itīs only 1vs1, so the form must be dynamic.

    On the first page there are as many select-boxes as we have players in the DB. Each select-box contains a list of all playernames with the id as value. Next to this box I have another box with the available teams (1 - number of players in DB).

    After submitting the form I would like to use something more handy than loop through an enumeration and compare each string to "playerID[" + i + "]".

    Is it possible in JSP? Look above - itīs a 2minutes proggie in php.
    Last edited by DaHipster; July 22nd, 2003 at 03:13 AM.
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2003
    Posts
    78
    Rep Power
    12
    As far as I know you have to code for each element name that is passed to the servlet/jsp either using an enumeration or by using the elements name.

    Well, in the first page's form you could pass a comma delimeted list of the player ids then you could loop through them and get each specific variable.

    An example...
    HTML input elements are:
    playerIds (1,2,3,4,5)
    playerId_1,
    playerId_2,
    .
    .
    .
    playerId_5

    Code:
    StringTokenizer playerIds = new StringTokenizer(request.getParameter("playerIds"), ",");
    String[] playerIdsArray = new String[playerIds.countTokens()];
    String[] playerValuesArray = new String[playerIds.countTokens()];
    int i = 0;
    while (playerIds.hasMoreTokens()) {
      playerIdsArray[i] = "playerId_" + playerIds.nextToken();
      playerValuesArray[i] = request.getParameter(playerIdsArray[i]);
      i++;
    }
    There is no way that I know of to generate a java variable on the fly. Sorry.
  12. #7
  13. No Profile Picture
    Clueless llama
    Devshed Regular (2000 - 2499 posts)

    Join Date
    Feb 2001
    Location
    Lincoln, NE. USA
    Posts
    2,353
    Rep Power
    117
    As I understand it you are making a variable number of inputs and submitting them to a page. The receiving page does not know how many fields it will receive but does know what the name of the fields are. Is this correct?

    If so, you could do something like this:
    Code:
      for ( int i = 0; i < whatever; i ++ ) {
        out.println("<input type='text' name='fields' value='" + fields[i] + "'>");
      }
    -----------------------------
    
    String[] fields = request.getParameterValues("fields");
    if(fields != null) {
      for (int i = 0; i < fields.length; i ++) {
        out.println(fields[i] + "<BR>\n");
      }  
    }
    To make an array of input variables in php you use square brackets in the inputs name. There is no such requirement in java. All inputs on a page with the same name will be returned by using the getParameterValues method of the request. Does this answer your question?

IMN logo majestic logo threadwatch logo seochat tools logo