B4J Question Read an HTML form data array

avalle

Active Member
Licensed User
Longtime 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
B4X:
Dim mydata As String = req.GetParameter("mydata")
to read a single parameter "mydata".

My payload also contains multiple parameters like
B4X:
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
 

stevel05

Expert
Licensed User
Longtime 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.

B4X:
    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
 
Upvote 0

avalle

Active Member
Licensed User
Longtime 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).
 
Upvote 0

stevel05

Expert
Licensed User
Longtime 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.
 
Upvote 0
Top