B4J Question Read an HTML form data array

Discussion in 'B4J Questions' started by avalle, Jul 1, 2018.

Tags:
  1. avalle

    avalle Active Member Licensed User

    Hi
    I need to read an array of parameters from the payload of an HTML form that is POST-ed to my B4J server.
    I normally use
    Code:
    Dim mydata As String = req.GetParameter("mydata")
    to read a single parameter "mydata".

    My payload also contains multiple parameters like
    Code:
    text[0]=thisIsTextElementOne&text[1]=thisIsTextElementTwo&text[2]=thisIsTextElement&...
    What should I do to read these "text[n]" parameters into a "text" array?

    Thanks
    Andrea
     
  2. stevel05

    stevel05 Expert Licensed User

    I haven't used this, but I assume you the object req is a ServletRequest Object.

    It seems that code like this would do what you want. You will need to provide the parameter names in the order you want them stored.

    Code:
    Dim Params As String = Array As String("ParamName1","ParamName2","ParamName3")
        
    Dim Data(Params.Length) As String
        
    For i = 0 To Params.Length -1
            Data(i) = Req.GetParameter(Params(i))
        
    Next
    Or look at using the ParameterMap
     
  3. avalle

    avalle Active Member Licensed User

    Thanks Stevel, however your code assumes a predefined list of parameters ("ParamName1", 2..., 3...).
    In my case the list of parameters is variable and unpredictable because the client can specify 0, 1 or multiple parameters "text[n]".

    I could use your code with a high enough number of parameters, but I would like to use a more flexible and efficient code that reads the existing parameters in a loop (or For Each).
     
  4. stevel05

    stevel05 Expert Licensed User

    Will the ParameterMap not work for you? You wouldn't need top know the parameter names in advance and you could use a For Each on the keys to iterate over the Map.
     
  5. avalle

    avalle Active Member Licensed User

    Yes it worked, thanks!
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice