Using TIdHTTP in Delphi to submit form and retrive data (not the usual)
delphi, html, indy
Solution
You are posting your data to this URL, which is wrong:
IdHTTP1.Post('www.someurl.co.uk', Params, Response);
That is why you are getting the initial page back again - that is what you are actually asking for!
You need to post to this URL instead:
IdHTTP1.Post('http://www.someurl.co.uk/results.php', Params, Response);
And BTW, you do not need the `TStringStream` at all, `Post()` can return the string data for you:
procedure TForm1.Button2Click(Sender: TObject);
var
Params: TStringList;
begin
Params := TStringList.Create;
try
Params.Add('thedata=' + 'MyString');
try
memo1.Text := IdHTTP1.Post('www.someurl.co.uk', Params);
except
on E: Exception do
begin
ShowMessage('Error: ' + E.Message);
end;
end;
finally
Params.Free;
end;
end;
Problem
There exists a web page (www.someurl.co.uk) that, amongst other stuff, has inside it a form containing an input field and an image. In a browser, when the user enters a string in the input field and clicks the image, the form is submitted and another page is generated (www.someurl.co.uk/results.php) containing, amongst other things, a table containing some results. I need Delphi to fill in the string in the form on the first page, submit the form and get back the contents of a couple of cells from the table on the results page. I have used the TIdHTTP component to post data to the form using the code below but the text I see in the memo box afterwards is simply the html of the initial page (www.someurl.co.uk). Please can somebody help me with the correct code to get the the values BlaBla_A, BlaBla_C, BlaBla_C and BlaBla_D from the results page - and to know which is which? (Although I'm happy with Delphi, I don't know much about web pages and HTML and this is my first time using the Indy component) This is the code I am using at the moment ``` procedure TForm1.Button2Click(Sender: TObject); var Response: TStringStream; Params: TStringList; begin Params := TStringList.Create; try Params.Add('thedata=' + 'MyString'); Response := TStringStream.Create(''); try IdHTTP1.Post('www.someurl.co.uk', Params, Response); memo1.Text := Response.DataString; except on E: Exception do begin showmessage('Error: ' + E.Message); end; end; finally Params.Free; Response.Free; end; end; ``` The important bit of the data entry/submit page (www.someurl.co.uk) looks like this ``` </div> <h3>Enter the data you want to look for then click FIND</h3> <form action="results.php" method="post" name="form1" target="_parent" id="form1" onsubmit="if (document.getElementById('text').value=='' || document.getElementById('text').value=='ENTER DATA') {alert ('Please enter data first then click FIND'); return false;} else return true;"> <input name="thedata" type="text" id="text" value="ENTER DATA" onclick="effectClear(this.id);this.value = '';" class="validate[required]"/> <input type="image" src="../images/find.png" name="image" id="homebutton" /> </form> </div> ``` and the important bit of the results page (www.someurl.co.uk/results.php) looks like this ``` <div id="results_details"> <table id="results_details_table" border="0"> <tr> <td class="result_general"><h2>Here are your results</h2><br/></td> <td class="result_info"> <label>Result_A</label><br/>BlaBla_A<br/> <label>Result_B</label><br/>BlaBla_B<br/> </td> <td class="result_info"> <label>Result_C</label><br/>BlaBla_C<br/><br/> <label>Result_D</label><br/>BlaBla_D<br/><br/> </td> </tr> </table> </div> ```