Catch html form data

Share your advanced PureBasic knowledge/code with the community.
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Catch html form data

Post by utopiomania »

Code updated For 5.20+

This is based on Sparkies contribution here: viewtopic.php?t=14431 and is
one method of catching the data posted from a form in the webgadget.

It certainly has it's limitations, but until a proper solution shows up it can be used.

Save this as C:\form.htm:

Code: Select all

<html><head><meta http-equiv='MSThemeCompatible' content='yes'></head>
<body scroll=auto link=blue vlink=blue alink=green> 
<form method='get' action='http://formdata'> 

<p>Single line text input field: 
<br><input type=text name=text value='Default text' size=40 maxlength=40> 

<p>Password input field: 
<br><input type=password name=password value=secret size=40 maxlength=40> 

<p>Multiple line input field: 
<br><textarea name=textarea rows=4 cols=30></textarea> 

<p>Single option selection: 
<br><select name=option> 
<option value=Option1>Option 1 
<option selected value=Option2>Option 2 
<option value=Option3>Option 3 
<option value=Option4>Option 4 
</select> 

<p>Multiple options selection: 
<br><select multiple name=multiple size=3> 
<option value=Multiple1>Multiple 1 
<option selected value=Multiple2>Multiple 2 
<option value=Multiple3>Multiple 3 
<option value=Multiple4>Multiple 4 
</select> 

<p>Filename selection: 
<br><input type=file name=file size=40 maxlength=40> 

<p>Check boxes: 
<br><input type=checkbox name=checked value=check1 checked>Checkbox1 
<input type=checkbox name=checked value=check2>Checkbox1 

<p>Grouped radio buttons: 
<br><input type=radio name=radio value=radio1 checked group>Radiobutton 1 
<input type=radio name=radio value=radio2>Radiobutton 2 

<p>Resets the form to its default values: 
<br><input type=reset value=Default> 

<p>Multiple submit buttons, click to see the form data: 
<br><input type=submit name=Submit value=Submit1> 
<input type=submit name=Submit value=Submit2> 
<input type=submit name=Submit value=Submit3> 

</form></body></html>
Then load and run this code, and click one of the submit buttons. After a short delay the data posted from the form appears.
The posted data need to be decoded, I'll add that shortly. EDIT: Added.

Code: Select all

Global Dim Name.s(20)
Global Dim Value.s(20)
Global Dim NV.s(20)

Procedure SplitPostedData(Url.s)
  ;Splits posted data into name/value pairs
  Hex.s="0123456789ABCDEF"
  Url = Mid(Url, FindString(Url, "?", 1) + 1, Len(Url) - FindString(Url, "?", 1))
  NV(Y) = ""
  For X = 1 To Len(Url)
    Ch.s = Mid(Url, X, 1)
    If (Ch = "&")
      Y + 1: Ch = "": NV(Y) = ""
    Else
      If Ch = "+"
        Ch = " "
      EndIf
      If Ch = "%"
        X + 1: H = (FindString(Hex, Mid(Url, X, 1), 1) - 1) * 16
        X + 1: H + FindString(Hex, Mid(Url, X, 1), 1) - 1
        Ch = Chr(H)
      EndIf
    EndIf
    NV(Y) + Ch
  Next X
  Y = 0
  While NV(Y)
    Name(Y) = Mid(NV(Y), 1, FindString(NV(Y), "=", 1) - 1)
    Value(Y) = Mid(NV(Y), FindString(NV(Y), "=", 1) + 1, Len(NV(Y)) - FindString(NV(Y), "=", 1))
    Y + 1
  Wend
EndProcedure

If OpenWindow(0, 0, 0, 600, 800, "", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)
  ;       If CreateGadgetList(WindowID())
  WebGadget(0, 0, 0, 600, 800, "file://c:\form.htm")
  ;Expose the IWebBrowser2 object
  WebObject.IWebBrowser2 = GetWindowLong_(GadgetID(0), #GWL_USERDATA)
  Repeat
    Event = WaitWindowEvent()
    If isBusy
      WebObject\get_ReadyState(@isReady)
    EndIf
    If isReady > 1
      ;Page loaded
      Url.s = GetGadgetText(0)
      If  FindString(Url, "formdata", 1)
        SetGadgetState(0, #PB_Web_Back)
        SplitPostedData(Url)
        I = 0
        While Name(I)
          MessageRequester("Form data split into name/value Pairs:", Name(I) + "=" + Value(I))
          I + 1
        Wend     
      EndIf
    EndIf
    ;Get the load state of WebGadget
    WebObject\get_busy(@isBusy)
  Until Event = #PB_Event_CloseWindow
  ;       EndIf
EndIf

End

Last edited by utopiomania on Tue Oct 25, 2005 8:33 pm, edited 4 times in total.
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post by rsts »

Nice one.

Thanks
User avatar
utopiomania
Addict
Addict
Posts: 1655
Joined: Tue May 10, 2005 10:00 pm
Location: Norway

Post by utopiomania »

I just fixed a problem with multiple submissions, the code was updated.
Post Reply