Post form Data

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
LVL 22
Mohammed NasmanSoftware DeveloperAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Eddie ShipmanAll-around developerCommented:
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
Mohammed NasmanSoftware DeveloperAuthor Commented:
EddieShipman,

it gave the same result as my code :-(
0
Eddie ShipmanAll-around developerCommented:
What result? Can you post the GetOptions.aspx code?
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

Mohammed NasmanSoftware DeveloperAuthor Commented:

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
Eddie ShipmanAll-around developerCommented:
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
geobulCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Eddie ShipmanAll-around developerCommented:
Damn, why didn't I see that, too.  I don't have a setup to test this with aspx, however.
0
Eddie ShipmanAll-around developerCommented:
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
Mohammed NasmanSoftware DeveloperAuthor Commented:
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
Eddie ShipmanAll-around developerCommented:
I'd like to know what you are expecitng ias a result because your aspx page does not return a result.
0
Mohammed NasmanSoftware DeveloperAuthor Commented:
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
geobulCommented:
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
Eddie ShipmanAll-around developerCommented:
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
Mohammed NasmanSoftware DeveloperAuthor Commented:
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
Eddie ShipmanAll-around developerCommented:
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
geobulCommented:
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
Eddie ShipmanAll-around developerCommented:
On that note, did you try my other example for submitting the form above?

0
Mohammed NasmanSoftware DeveloperAuthor Commented:
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
Eddie ShipmanAll-around developerCommented:
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
Mohammed NasmanSoftware DeveloperAuthor Commented:
sorry I forgot this questions
Thanks for your help
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Delphi

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.