Question

I'm making a Delphi XE5 VCL Forms Application and there is a TIdHTTPServer on the main form and a CommandGet of the IdHTTPServer procedure:

procedure TForm1.IdHTTPServerCommandGet(AContext: TIdContext;
  ARequestInfo: TIdHTTPRequestInfo; AResponseInfo: TIdHTTPResponseInfo);
var pageContent: TStringList;
begin
if pos('profile&userName=', ARequestInfo.UnparsedParams) > 0 then
   begin
      pageContent:= TStringList.Create;
      try
         pageContent.Add('<html>');
         pageContent.Add('<head>');
         pageContent.Add('<title>Profile</title>');
         pageContent.Add('</head>');
         pageContent.Add('<body>');
         pageContent.Add('<h1>Profile<h1>');
         pageContent.Add
            ('<input id="subjects" value="Subjects" type="button"/>');
         pageContent.Add('<div id="table-content">');
         pageContent.Add('</div>');
         pageContent.Add('</body>');
         pageContent.Add('</html>');
         AResponseInfo.ContentText := pageContent.Text;
      finally
         pageContent.Free;
      end;
  end;
end;

What I want to do is when the user clicks the button "Subjects" to call this function that generates a HTML table with students courses and marks. The function reads data from the database and generates a table. I want to avoid reloading the whole page (I try to insert the table in the div tags).

Était-ce utile?

La solution

For a partial page refresh, you can use JavaScript / Ajax, for example with a Framework like jQuery. There are many examples on the web for dynamic page updates with Ajax.

The Ajax call submits the request to the HTTP server as a HTTP GET or POST request. The code in IdHTTPServerCommandGet checks if the request matches the URL and HTTP method, and then builds and returns partial HTML code.

On the client side, the JavaScript code places the received partial HTML in the specified document tree location.

See for example:

https://stackoverflow.com/a/3644604/80901

Update: a jQuery Ajax based demo can be found at http://www.habariwebcomponents.de/demo/ajaxstats.html with Delphi source (based on my HTTP service library for Delphi and Free Pascal)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top