Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I write the client side Delphi code to send a request and process the returned data?

Delphi, PHP and MySQL

How do I write the client side Delphi code to send a request and process the returned data?

by  towerbase  Posted    (Edited  )
Let's recap. Your Delphi code has to send a request to the web server to ask for a list of people having a birthday on a specific date. Your code has to then process the returned data and display it on a form. For the purposes of this exercise we will put the list of names in a StringList.

So the request that we wish to send to the server is something like:

http://www.towerbase.34sp.com/birthdays.php?day=24&month=07

How do we send that to a web server? It is surprisingly easy if you have the Indy components (supplied with Delphi 6 and 7). The hardest part is finding the right Indy component because there are dozens of them!

The component you want is TIdHTTP which is on the Indy Clients tab. It's icon is a blue globe and is 11th from the left in my IDE.

Drop this on your form. Set the name property to HTTP. Drop a TButton on the form and in the OnClick handler for the button code the following:
Code:
const
  URL = 'http://www.towerbase.34sp.com/birthdays.php?day=%d&month=%d';
var
  command: string;
  response: string;
begin
  command := Format ( URL, [ DayOf(Now), MonthOf(Now) ] );
  response := HTTP.Get ( command );
  ExtractNames ( response );
end;

To keep things simple, I've assumed you want a list of people whose birthday is today. But you could make it more flexible by dropping a Calender on the form and using that to select a date.

The response variable will contain the entire HTML for the web page. In particular it will contain the names we want embedded within paragraph tags.

The ExtractNames routine would extract the names from within these tags and add them to a string list. This task is made particularly easy because each <p> starts on a new line, followed by a line containing the name followed by a line containing the </p>.

If you want to try this out then you can use the URL supplied above.
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top