B4J Question Cannot get Data from a form which was working

Peter Lewis

Well-Known Member
Licensed User
Longtime User
Hi,
I have been looking for a few hours why this worm will not save data.
I checked on the HTML side and it looks perfect. How I did this was to past the form to https://httpbin.org/post
The result is
{
"args": {},
"data": "",
"files": {},
"form": {
"addressInput": "95 Ridgeside Road, Durban North",
"contactInput": "Peter Lewis",
"descriptionTextarea": "description test",
"emailInput": "peter@praxos-software.co.za",
"mallnameInput": "Gateway",
"municipalitySelect": "Ethekwini",
"ownerSelect": "9f8ff690-5034-46a0-bc0c-c4a3b299c6ca",
"phoneInput": "0724533919",
"postalcodeInput": "4051",
"postersInput": "20",
"provinceSelect": "KWAZULU-NATAL",
"retailshopsInput": "100",
"suburbSelect": "Durban NU",
"websiteInput": "https://www.home.co.za"
},
"headers": {
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.7",
"Accept-Encoding": "gzip, deflate, br, zstd",
"Accept-Language": "en-US,en;q=0.9,en-ZA;q=0.8",
"Cache-Control": "max-age=0",
"Content-Length": "423",
"Content-Type": "application/x-www-form-urlencoded",
"Dnt": "1",
"Host": "httpbin.org",
"Origin": "http://127.0.0.1:5000",
"Priority": "u=0, i",
"Referer": "http://127.0.0.1:5000/",
"Sec-Ch-Ua": "\"Not:A-Brand\";v=\"99\", \"Google Chrome\";v=\"145\", \"Chromium\";v=\"145\"",
"Sec-Ch-Ua-Mobile": "?0",
"Sec-Ch-Ua-Platform": "\"Windows\"",
"Sec-Fetch-Dest": "document",
"Sec-Fetch-Mode": "navigate",
"Sec-Fetch-Site": "cross-site",
"Sec-Fetch-User": "?1",
"Upgrade-Insecure-Requests": "1",
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/145.0.0.0 Safari/537.36",
"X-Amzn-Trace-Id": "Root=1-69b6dd10-5a79734210704ebc37600f25"
},
"json": null,
"origin": "169.1.***.***",
"url": "https://httpbin.org/post"
}

When I post to the server, I can see the length is different depending on what I type

clsMallInfoAdd
length 396
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@148891937 cs=HttpChannelState@503fbbc6{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@55f45b92 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Methods POST

But the map is not filled. I used this code to read it before and populate the DB

B4X:
    Dim query As String = utils.prepareInsertQuery($"
    INSERT INTO [ledposter].[dbo].[malls](
     [mallID]
    ,[mallName]
    ,[province]
    ,[city]
    ,[postalcode]
    ,[phone]
    ,[email]
    ,[ownerID]
    ,[suburb]
    ,[description]
    ,[retailshops]
    ,[posters]
    ,[status]
      ,[mtraffic]
 
            )"$)
    
    Dim guidST As JavaObject
    guidST.InitializeStatic("java.util.UUID")
    Dim guid As JavaObject = guidST.RunMethod("randomUUID",Null)
    Dim guidStr As String = guid.RunMethod("toString",Null)
    
        Dim params(query.Length - query.Replace("?","").Length) As String
    params(0) = guidStr
    params(1) = req.GetParameter("mallnameInput")
    params(2) = req.GetParameter("provinceSelect")
    params(3) = req.GetParameter("municipalitySelect")
    params(4) = req.GetParameter("postalcodeInput")
    params(5) = req.GetParameter("phoneInput")
    params(6) = req.GetParameter("emailInput")
    params(7) = req.GetParameter("ownerSelect")
    params(8) = req.GetParameter("suburbSelect")
    params(9) = req.GetParameter("descriptionTextarea")
    params(10) = req.GetParameter("retailshopsInput")
    params(11) =req.GetParameter("postersInput")
    params(12) = "unknown"
    params(13) ="0"
 
    sql.execnonquery2(query,params)

It seems like the data is coming through but I cannot find the method of extracting it

<form method="POST" action="/malladd">
</form>

Maybe it is a Sunday problem.

I am using HTMX to swap out the Select Areas and also to SWAP out the Searchable Fields.. I tried Choices.js but found it was not working for me as I had more than one field to use the plugin and it would not support that. So I eventually used datatables



B4X:
                                    <div class="col-lg-4">
                                                                            <label for="provinceSelect">Province</label>
                                                                            <br>
                                            <input list="provinceSelectlist" name = "provinceSelect" autocomplete="nope" hx-get="/muni"  hx-target="#municipalitySelectlist" placeholder="Search Province">               
                                                                            <datalist id="provinceSelectlist">
                                                                                  <#list  provincetable as provincetables>
                                                                                        <option value="${provincetables.province}" >
                                                                                  </#list>
                                                                            </datalist>
                                                                                    
                                                                            
                                                                        </div>
                                                                                            
                                                        <!-- State -->
                                                                        <div class="col-lg-4">
                                                                        <label for="municipalitySelect">Municipality</label>
                                                                        <br>
                                                                        <input list="municipalitySelectlist" name = "municipalitySelect"  autocomplete="nope" hx-get="/suburb"  hx-target="#suburbSelectlist" placeholder="Search Municipality">
                                                                                
                                                                            <datalist id="municipalitySelectlist">
                                                                                    <#list  municipalitytable as municipalitytables>
                                                                                        <option value="${municipalitytables.city}">
                                                                                    </#list>
                                                                            </datalist>
                                                                                
                                                                            
                                                                        </div>
                                                                        
                                                                        <div class="col-lg-4">
                                                                            <label for="suburbSelect">Suburb</label>
                                                                            <br>
                                                                            <input list="suburbSelectlist" name = "suburbSelect" autocomplete="nope" placeholder="Search Suburb">
                                                                                <datalist id="suburbSelectlist">
                                                                                            <#list  suburbtable as suburbtables>
                                                                                                <option value="${suburbtables.suburb}" >
                                                                                            </#list>
                                                                                </datalist>
                                                                        </div>



Thank you
 
Last edited:
Solution
Thank you to @aeric who spent time with me trying to find the solution
I have narrowed it down to the culprit which I still do not understand why but here it goes.

We added this line at the begin of the handle

B4X:
    Dim params As Map = Req.ParameterMap

It solved the problem which is crazy because we are not using the params variable

1773677000541.png
1773677023903.png

If I comment out this line then it saving fields of the form to the database does not work

I really hope there is an explanation because I like to know why, it improves my thought process for future problems.

Peter Lewis

Well-Known Member
Licensed User
Longtime User
1. If we try to call /muni using hx-get, it should make a GET request and the server should return a html text response such as:
B4X:
<input list="provinceSelectlist" name="provinceSelect" autocomplete="nope"
      hx-get="/muni"
      hx-target="#municipalitySelectlist" placeholder="Search Province">
<datalist id="provinceSelectlist">
      <option value="province 1">
      <option value="province 2">
      <option value="province 3">
</datalist>
But you said the web page works then you can ignore this part.

2. What is the code at line 30?
B4X:
Error occurred on line: 30 (clsmunicipality)

3. Make a test

B4X:
<form method="POST" action="/malladd">
      <input type="hidden" name="province" id="provinceInput" value="TEST">
      <button type="submit" class="btn btn-primary">Submit</button>
</form>

B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = req.Method
    Log($"${Request.Method}: ${Request.RequestURI}"$)
    If Method.EqualsIgnoreCase("POST") Then
        Dim province As String = Request.GetParameter("province")
        Log($"province=[${province}]"$)

4. Can you temporary commented the redirect line?
B4X:
resp.SendRedirect("/malladd")
It seems this is a GET request and share the same URI with POST.
Line 30 is giving an error because the input is null

 
Upvote 0

Peter Lewis

Well-Known Member
Licensed User
Longtime User
1. If we try to call /muni using hx-get, it should make a GET request and the server should return a html text response such as:
B4X:
<input list="provinceSelectlist" name="provinceSelect" autocomplete="nope"
      hx-get="/muni"
      hx-target="#municipalitySelectlist" placeholder="Search Province">
<datalist id="provinceSelectlist">
      <option value="province 1">
      <option value="province 2">
      <option value="province 3">
</datalist>
But you said the web page works then you can ignore this part.

2. What is the code at line 30?
B4X:
Error occurred on line: 30 (clsmunicipality)

3. Make a test

B4X:
<form method="POST" action="/malladd">
      <input type="hidden" name="province" id="provinceInput" value="TEST">
      <button type="submit" class="btn btn-primary">Submit</button>
</form>

B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = req.Method
    Log($"${Request.Method}: ${Request.RequestURI}"$)
    If Method.EqualsIgnoreCase("POST") Then
        Dim province As String = Request.GetParameter("province")
        Log($"province=[${province}]"$)

4. Can you temporary commented the redirect line?
B4X:
resp.SendRedirect("/malladd")
It seems this is a GET request and share the same URI with POST.


I put in the simple form and posted it, on the server I got


clsMallInfoAdd
length 13
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@1789282489 cs=HttpChannelState@125c082e{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@584f5497 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Mall Name
Methods POST
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
1. I suggest add:
B4X:
Log(req.ParameterMap)
It may not been initialized so all values are empty.

2. Also try to use:
B4X:
Log(req.GetParameter("mallInput"))
* assuming there is an input field with attribute name = "mallInput"
 
Upvote 0

Peter Lewis

Well-Known Member
Licensed User
Longtime User
1. If we try to call /muni using hx-get, it should make a GET request and the server should return a html text response such as:
B4X:
<input list="provinceSelectlist" name="provinceSelect" autocomplete="nope"
      hx-get="/muni"
      hx-target="#municipalitySelectlist" placeholder="Search Province">
<datalist id="provinceSelectlist">
      <option value="province 1">
      <option value="province 2">
      <option value="province 3">
</datalist>
But you said the web page works then you can ignore this part.

2. What is the code at line 30?
B4X:
Error occurred on line: 30 (clsmunicipality)

3. Make a test

B4X:
<form method="POST" action="/malladd">
      <input type="hidden" name="province" id="provinceInput" value="TEST">
      <button type="submit" class="btn btn-primary">Submit</button>
</form>

B4X:
Sub Handle (req As ServletRequest, resp As ServletResponse)
    Request = req
    Response = resp
    Method = req.Method
    Log($"${Request.Method}: ${Request.RequestURI}"$)
    If Method.EqualsIgnoreCase("POST") Then
        Dim province As String = Request.GetParameter("province")
        Log($"province=[${province}]"$)

4. Can you temporary commented the redirect line?
B4X:
resp.SendRedirect("/malladd")
It seems this is a GET request and share the same URI with POST.
I took out all the code in the html EXCEPT for the bootstrap and tried again
1. I suggest add:
B4X:
Log(req.ParameterMap)
It may not been initialized so all values are empty.

2. Also try to use:
B4X:
Log(req.GetParameter("mallInput"))
* assuming there is an input field with attribute name = "mallInput"
I took out all the code in the html EXCEPT for the bootstrap and tried again



Same Result

clsMallInfoAdd
length 13
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@521466380 cs=HttpChannelState@7daa61f3{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@62315f22 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Mall Name
Methods POST
 
Upvote 0

Peter Lewis

Well-Known Member
Licensed User
Longtime User
1. I suggest add:
B4X:
Log(req.ParameterMap)
It may not been initialized so all values are empty.

2. Also try to use:
B4X:
Log(req.GetParameter("mallInput"))
* assuming there is an input field with attribute name = "mallInput"

That was always there

console result

clsMallInfoAdd
length 13
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@521466380 cs=HttpChannelState@7daa61f3{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@62315f22 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Mall Name
Methods POST
 
Upvote 0

Peter Lewis

Well-Known Member
Licensed User
Longtime User
1. I suggest add:
B4X:
Log(req.ParameterMap)
It may not been initialized so all values are empty.

2. Also try to use:
B4X:
Log(req.GetParameter("mallInput"))
* assuming there is an input field with attribute name = "mallInput"
B4X:
<!--
* Author: Peter Lewis
* Product Name: LED POSTER ADs
* Version: 5
* Website:  https://www.ledposter.co.za
* Contact: support@ledposter.co.za
-->
<!DOCTYPE html>
<html lang="en">

        <head>
             <meta charset="utf-8">
        <title>Dashboard | LED POSTER ADs</title>
          <meta name="viewport" content="width=device-width, initial-scale=1">
            <meta name="description" content="LED Posters high quality media display devices along with the facility to connect to our Advertising platform to earn advertising revenue">
            <meta name="keywords" content="LED Poster, Marketing poster, digital marketing, LED Poster Averts">
            <meta name="author" content="Peter Lewis">

    
        </head>

<body>
    <!-- Begin page -->
    <div class="wrapper">
                <form method="POST" action="/malladd">
                      <input type="hidden" name="province" id="provinceInput" value="TEST">
                      <button type="submit" class="btn btn-primary">Submit</button>
                </form>
                            
   </div>
            <!-- Footer Start -->
             <footer class="footer">
              
            </footer>
            <!-- end Footer -->
    </div>
        <!-- ============================================================== -->
        <!-- End of Main Content -->
        <!-- ============================================================== -->


</body>
</html>

Is now the HTML file , Nothing else

1773661608447.png


just with a submit button

clsMallInfoAdd
length 13
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@955443582 cs=HttpChannelState@779dfe55{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@323659f8 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Mall Name
Methods POST
clsMallInfoAdd
length 13
Type application/x-www-form-urlencoded
Full Request URL http://127.0.0.1:5000/malladd
Input Stream (HttpInput) HttpInput@1062181581 cs=HttpChannelState@46baf579{s=HANDLING rs=BLOCKING os=OPEN is=IDLE awp=false se=false i=true al=0} cp=org.eclipse.jetty.server.BlockingContentProducer@4bf324f9 eof=false
Methods POST
Request URI /malladd
Secure ? false
(MyMap) {}
Mall Name
Methods POST

same result
B4X:
Sub Handle(req As ServletRequest, resp As ServletResponse)
        #if debug
    Main.templates.clearTemplateCache
    #End If   
    Dim sql As SQL = utils.pool.GetConnection
    Log("clsMallInfoAdd")
    Log("length "&req.ContentLength)
    Log("Type "&req.ContentType)
    Log("Full Request URL "&req.FullRequestURI)
    Log("Input Stream "&req.InputStream)
    Log("Methods "&req.Method)
    Log("Request URI "&req.RequestURI)
    Log("Secure ? "&req.Secure)
    Log(req.ParameterMap)
    Dim root As Map
    root.Initialize
    Log("Mall Name "&req.GetParameter("province"))
    Log("Methods "&req.Method)
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
A little hard to help since we don't see the code in the handle sub or your custom class.
I tested with a simple project here.
 

Attachments

  • ParameterTest.zip
    2 KB · Views: 7
Upvote 1

Peter Lewis

Well-Known Member
Licensed User
Longtime User
A little hard to help since we don't see the code in the handle sub or your custom class.
I tested with a simple project here.
I do not understand how but all of a sudden it is working again with my original code. I did leave your sub inside which I will take out 1 line at a time or could it be IRAN hackers


 
Upvote 0

Peter Lewis

Well-Known Member
Licensed User
Longtime User
Thank you to @aeric who spent time with me trying to find the solution
I have narrowed it down to the culprit which I still do not understand why but here it goes.

We added this line at the begin of the handle

B4X:
    Dim params As Map = Req.ParameterMap

It solved the problem which is crazy because we are not using the params variable


If I comment out this line then it saving fields of the form to the database does not work

I really hope there is an explanation because I like to know why, it improves my thought process for future problems.
 
Last edited:
Upvote 0
Solution
Top