[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Post form Data

Posted on 2004-11-11
21
Medium Priority
?
248 Views
Last Modified: 2010-04-05
Hello

  I have web page that have some options, when I choose one of the option it will show result in another page,
what I want to do is dynamically choose one of the option and get the result, then parse that data, the parse part isn't the problem, but I couldn't make the first part to work, even I did that many times before :-(,

here's my code:

procedure TForm1.Button2Click(Sender: TObject);
var
  S : TStringList;
  sr : TStringStream;
begin
  S := TStringList.Create;
  sr := TStringStream.Create('');
  S.Add('OptionsList=2');
  IdHTTP1.Request.ContentType := 'application/x-www-form-urlencoded';
  try
    idhttp1.Post('http://localhost:8080/WebApp/GetOptions.aspx',s,sr);
    Memo1.text := sr.DataString;
  finally
    S.free;
    sr.Free;
  end;
end;

and here's the options page

<HTML>
      <HEAD>
            <title>Options Page</title>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <form name="Form1" method="post" action="GetOptions.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTM1Nzc4NTA1MDs7PpycGeYF6PG5oTx5j+MZPUxhKXfB" />

                  <table id="OptionsList" border="0" style="width:186px;Z-INDEX: 101; LEFT: 20px; POSITION: absolute; TOP: 19px">
      <tr>
            <td><input id="OptionsList_0" type="radio" name="OptionsList" value="0" /><label for="OptionsList_0">Option 0</label></td>
      </tr><tr>
            <td><input id="OptionsList_1" type="radio" name="OptionsList" value="1" /><label for="OptionsList_1">Option 1</label></td>
      </tr><tr>
            <td><input id="OptionsList_2" type="radio" name="OptionsList" value="2" /><label for="OptionsList_2">Option 2</label></td>
      </tr>
</table>
                  <input type="submit" name="Button1" value="Button" id="Button1" style="Z-INDEX: 102; LEFT: 73px; POSITION: absolute; TOP: 113px" />
            </form>
      </body>
</HTML>

any Idea?

Regards,
Mohammed
0
Comment
Question by:Mohammed Nasman
  • 10
  • 7
  • 3
20 Comments
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12567525
Have you tried doing it like this:

procedure TForm1.Button2Click(Sender: TObject);
var
  LStream:                     TMemoryStream;
  LParams:                      TStringStream;
begin
  LStream := TMemoryStream.Create;
  LParams := TStringStream.Create('');
  try
    with IdHTTP1 do
    begin
      LParams.WriteString(URLEncode('OptionsList=' + '2'));
      Request.ContentType := 'application/x-www-form-urlencoded';
      try
        Post('http://localhost:8080/WebApp/GetOptions.aspx', LParams, LStream);
      except
        on E: Exception do
          ShowMessage('Error encountered during POST: ' + E.Message);
      end;
    end;
    LStream.WriteBuffer(#0' ', 1);
    LStream.Position := 0;
    Memo1.Lines.LoadFromStream(LStream);
  finally
    LStream.Free;
    LParams.Free;
  end;
end;
0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12571559
EddieShipman,

it gave the same result as my code :-(
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12571640
What result? Can you post the GetOptions.aspx code?
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12575041

I posed it above, here's it again

<HTML>
     <HEAD>
          <title>Options Page</title>
     </HEAD>
     <body MS_POSITIONING="GridLayout">
          <form name="Form1" method="post" action="GetOptions.aspx" id="Form1">
<input type="hidden" name="__VIEWSTATE" value="dDwtMTM1Nzc4NTA1MDs7PpycGeYF6PG5oTx5j+MZPUxhKXfB" />

               <table id="OptionsList" border="0" style="width:186px;Z-INDEX: 101; LEFT: 20px; POSITION: absolute; TOP: 19px">
     <tr>
          <td><input id="OptionsList_0" type="radio" name="OptionsList" value="0" /><label for="OptionsList_0">Option 0</label></td>
     </tr><tr>
          <td><input id="OptionsList_1" type="radio" name="OptionsList" value="1" /><label for="OptionsList_1">Option 1</label></td>
     </tr><tr>
          <td><input id="OptionsList_2" type="radio" name="OptionsList" value="2" /><label for="OptionsList_2">Option 2</label></td>
     </tr>
</table>
               <input type="submit" name="Button1" value="Button" id="Button1" style="Z-INDEX: 102; LEFT: 73px; POSITION: absolute; TOP: 113px" />
          </form>
     </body>
</HTML>

I mean by the result, the return string from the Post method is the code of GetOptions page before it's post method

I tried to compare the code with Get and post and it's the same, so it seems the Post doesn't execute

Thanks
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12576460
Sorry, but that doesn't LOOK like an aspx page, it looks like straight HTML. I don't have IIS/.Net
installed on my home machine. I may be able to test it on Monday.

0
 
LVL 17

Accepted Solution

by:
geobul earned 1400 total points
ID: 12585458
Hi,

Mohammed, there is another hidden input element in the form ("__VIEWSTATE") and perhaps you have to add it and its value to S before post. My thoughts are that the server part first checks if that hidden input exists and if its value is correct. If the above is not correct then the request (post) will not be executed.

Regards, Geo
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12585514
Damn, why didn't I see that, too.  I don't have a setup to test this with aspx, however.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12593847
Say, Mohammed, try something like this approach. I built this to be able
to submit forms in IE based on the form fields. I have yet to finish it but it is a start.


function FormSubmit
  ( AFormAction:           String
  ; AFormTitle:            String
  ; AFields:               Array of String
  ; AFieldValues:          Array of String
  ) : Boolean;
var
  ShellWindow: IShellWindows;
  WB: IWebbrowser2;
  spDisp: IDispatch;
  IDoc1: IHTMLDocument2;
  Form: IHTMLFormElement;
  Document: Variant;
  k, m: Integer;
  ovElements: OleVariant;
  i: Integer;
  IEFieldsCounter: Integer;
  btnElement: Integer;
begin
  ShellWindow := CoShellWindows.Create;
  // get the running instance of Internet Explorer
  for k := 0 to ShellWindow.Count do
  begin
    spDisp := ShellWindow.Item(k);
    if spDisp = nil then Continue;
    // QueryInterface determines if an interface can be used with an object
    spDisp.QueryInterface(iWebBrowser2, WB);
    if WB <> nil then
    begin
      WB.Document.QueryInterface(IHTMLDocument2, iDoc1);
      btnElement := -1;
      if iDoc1 <> nil then
      begin
        WB := ShellWindow.Item(k) as IWebbrowser2;
        Document := WB.Document;
        // if correct instance...
        if Pos(AFormTitle, Document.Title) <> 0 then
        begin
          // count forms on document and iterate through its forms
          IEFieldsCounter := 0;
          for m := 0 to Document.forms.Length - 1 do
          begin
            Form := IDispatch(Document.Forms.Item(m)) as IHTMLFormElement;
            if Pos(UpperCase(AFormAction), UpperCase(Form.action)) > 0 then
            begin
              ovElements := Document.forms.Item(m).elements;

              // iterate through elements
              for i := 0 to ovElements.Length -1 do
              begin
                try
                // if input fields found, try to fill them out
                  if (UpperCase(ovElements.item(i).type) = 'SUBMIT') or
                     (UpperCase(ovElements.item(i).type) = 'IMAGE') then
                  begin
                      btnElement := i;
                  end;
                  if not ( (UpperCase(ovElements.item(i).type) = 'CHECKBOX') or
                           (UpperCase(ovElements.item(i).type) = 'RADIO') ) then
                  begin
                    if (LowerCase(ovElements.item(i).name) = LowerCase(AFields[i])) then
                    begin
                        ovElements.item(i).Value := AFieldValues[i];
                        inc(IEFieldsCounter);
                    end;
                  end
                  else
                  begin
                    if StrToInt(AFieldValues[i]) = 1 then
                    begin
                      ovElements.item(i).Checked := True;
                    end
                    else
                    begin
                      ovElements.item(i).Checked := False;
                    end;
                    inc(IEFieldsCounter);
                  end;
                except
                  // failed...
                end;
              end; { for i...}
              if btnElement > -1 then
              begin
                ovElements.item(btnElement).click;
              end
              else
              begin
                Document.forms.Item(m).submit;
              end;
            end;
          end;  { for m }
        end;  {if Pos(AFormTitle, ...}
      end;  { idoc <> nil }
    end; { wb <> nil }
  end;  { for k }

end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  FormSubmit('GetOptions.aspx', {FormAction}
             'Options Page', {FormTitle}
             ['__VIEWSTATE', 'OptionsList', 'OptionsList', 'OptionsList'] {AFields},
             ['', '0', '0', '1'] {AFieldValues}
             );
end;
0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12618604
Hello Guys

  Sorry for late reply

geobul, I tried to add the hidden field, but it's the same, I feel Post method doesn't execute at all, it's always gave me the same result with Get method

EddieShipman, I tried your last code, it didn't do anything, how can I return the result?, sorry i'm not familiar with this way

Thanks
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12619097
I'd like to know what you are expecitng ias a result because your aspx page does not return a result.
0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12632428
EddieShipman
when post to server, it will add the selected option to session object, and then redirect to anther page, and it will show the result based on the selected option
0
 
LVL 17

Expert Comment

by:geobul
ID: 12638039
Hi,

Perhaps that hidden string changes every time you load that page, i.e. is some kind of session, status, authentication, etc. information. And you have to parse the source after loading the page and pass the current string value (not a constant value known beforehand).

Regards, Geo
0
 
LVL 26

Assisted Solution

by:EddieShipman
EddieShipman earned 600 total points
ID: 12644313
geobul: "Understanding ASP.NET View State"
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dnaspp/html/viewstate.asp

Mohammed, show some code to do that so we can test the entire thing.
0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12659973
geobul, that's what I did

EddieShipman, the page is very simple i just start with that just for testing before i start the project

here's the GetOptions.Aspx:

<%@ Page Language="vb" AutoEventWireup="false"

Codebehind="GetOptions.aspx.vb" Inherits="WebApp.WebForm1">
<HTML>
      <HEAD>
            <title>Options Page</title>
      </HEAD>
      <body MS_POSITIONING="GridLayout">
            <TABLE height="220" cellSpacing="0" cellPadding="0"

width="155" border="0" ms_2d_layout="TRUE">
                  <TR vAlign="top">
                        <TD width="155" height="220">
                              <form id="Form1" method="post"

runat="server">
                                    <TABLE height="138"

cellSpacing="0" cellPadding="0" width="208" border="0" ms_2d_layout="TRUE">
                                          <TR

vAlign="top">
                                                <TD

width="20" height="19"></TD>
                                                <TD

width="53"></TD>
                                                <TD

width="135"></TD>
                                          </TR>
                                          <TR

vAlign="top">
                                                <TD

height="94"></TD>
                                                <TD

colSpan="2">
                                                      

<asp:RadioButtonList id="OptionsList" runat="server" Width="186px"

EnableViewState="False">
                                                      

      <asp:ListItem Value="0">Option 0</asp:ListItem>
                                                      

      <asp:ListItem Value="1">Option 1</asp:ListItem>
                                                      

      <asp:ListItem Value="2">Option 2</asp:ListItem>
                                                      

</asp:RadioButtonList></TD>
                                          </TR>
                                          <TR

vAlign="top">
                                                <TD

colSpan="2" height="25"></TD>
                                                <TD>
                                                      

<asp:Button id="Button1" runat="server" Text="Button"

EnableViewState="False"></asp:Button></TD>
                                          </TR>
                                    </TABLE>
                              </form>
                        </TD>
                  </TR>
            </TABLE>
      </body>
</HTML>


========
the code behind of the above aspx page has this code only

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Select Case OptionsList.SelectedIndex
            Case 0
                Session("Option") = 0
            Case 1
                Session("Option") = 1
            Case 2
                Session("Option") = 2
        End Select
        Response.Redirect("http://localhost:8080/WebApp/ShowOption.aspx")
    End Sub
End Class
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12660417
Well, personally, I don't do ASP.net so I can't help you any more because I don'tknow what to do with
that VB code you posted.

Did you try to add this to your aspx page as outlined in the article I mentioned above?

  <%@Page EnableViewState="False" %>

0
 
LVL 17

Expert Comment

by:geobul
ID: 12662780
Eddie, thanks for the link. It requires background I don't have though ;-)

Mohammed, in a desperate attempt to be in assistance without having aspx experience here is another (perhaps stupid) shot in the dark:
Maybe you have to find a way to push that Button1 (or to simulate that in your request) instead of posting the form. The VB code seems like is being trigerred on that event instead of on post. I've worked with such regular HTML forms and CGI behind them which have behaved that way. Pushing the submit button worked while posting the form didn't.
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12665905
On that note, did you try my other example for submitting the form above?

0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 12674956
Eddie, I tried disable the EnableViewState before for the apsx page and also for all the controls, but the hidden field will exist

and I tried the other code too, and as I said above, I need to return the result of posting, but the code doesn't do that, so
seems this way will not work here, I will change my thinking then

geobul, I used this method with some asp pages and worked fine with me

How do you sumit the button?,

if you don't like the VB.Net code, I can change it to C# if you know that better, I used to work with both, but I made that page in hurry with VB.Not (oh .net ;))

Thanks for your time
0
 
LVL 26

Expert Comment

by:EddieShipman
ID: 12696379
No, I just don't do .NET anything, no need ot go changing on my account. I already showed code to "push" the button so he may have already tried that one.
0
 
LVL 22

Author Comment

by:Mohammed Nasman
ID: 13356454
sorry I forgot this questions
Thanks for your help
0

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Objective: - This article will help user in how to convert their numeric value become words. How to use 1. You can copy this code in your Unit as function 2. than you can perform your function by type this code The Code   (CODE) The Im…
Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
This Micro Tutorial will teach you how to add a cinematic look to any film or video out there. There are very few simple steps that you will follow to do so. This will be demonstrated using Adobe Premiere Pro CS6.
Loops Section Overview
Suggested Courses
Course of the Month20 days, 4 hours left to enroll

872 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question