Using Delphi As A Form

How do I make an application using Delphi to make it work like a web HTML form?

You know, like sending the data to the CGI script for processing and all that stuff.
jackjokerAsked:
Who is Participating?
 
intheConnect With a Mentor Commented:
Hi
i have a project that was done for me by epsylon when i wanted to learn this ,it uses the dbdemos ,biolife database and you can add,delete records fro it via the form,it will probably be easier to understand if i send you the whole project,you need it just leave your email address.
Regards Barry


// This method receives the content of a form and send it back to the client
procedure TWebModule1.WebModule1WebActionItem3Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var i: Integer;
begin
  // Request.ContentFields contains all info a client submitted in a form
  Response.Content := '';
  with Request.ContentFields do
    for i := 0 to Count - 1 do
      Response.Content := Response.Content +
        '<p>' + Strings[i] + '</p>';
end;


// This method sends a page with an html-form to the client.
// Depending on ContentFields.Values['button'] it sends the appropriate record.
procedure TWebModule1.WebModule1WebActionItem4Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
var b: String;
    err: TStringList;
    i: Integer;
begin
  b := Request.ContentFields.Values['button'];
  // If no button was pressed, just send the first record
  if b = '' then
  begin
    if Request.ContentFields.Values['record'] = '' then
      Table1.First
    else
      Table1.Locate('Species No', Request.ContentFields.Values['record'], []);
    Response.Content := DataSetPageProducer1.Content;
  end

  // If the 'Next' button was pressed, send the next record
  else if b = 'Next' then
  begin
    // Find the current record using the hidden html-form field 'record'
    Table1.Locate('Species No', Request.ContentFields.Values['record'], []);
    Table1.Next;
    Response.Content := DataSetPageProducer1.Content;
  end

  // If the 'Previous' button was pressed, send the previous record
  else if b = 'Previous' then
  begin
    // Find the current record using the hidden html-form field 'record'
    Table1.Locate('Species No', Request.ContentFields.Values['record'], []);
    Table1.Prior;
    Response.Content := DataSetPageProducer1.Content;
  end

  // If the 'Submit' button was pressed, Change the record
  else if b = 'Submit' then
  begin
    err := TStringList.Create;
    if Request.ContentFields.Values['record'] = '' then
      // Append a new record
      Table1.Append
    else begin
      // Edit the record found
      if Table1.Locate('Species No', Request.ContentFields.Values['record'], []) then
        Table1.Edit
      else
        Table1.Append;
    end;
    // Copy the ContentFields in the record fields
    try Table1.FieldByName('Species No').AsInteger  := StrToInt(Request.ContentFields.Values['speciesno']);
    except err.Add('Error in ''Species No'''); end;
    try Table1.FieldByName('Category').AsString     := Request.ContentFields.Values['category'];
    except err.Add('Error in ''Category'''); end;
    try Table1.FieldByName('Common_Name').AsString  := Request.ContentFields.Values['commonname'];
    except err.Add('Error in ''Common_Name'''); end;
    try Table1.FieldByName('Species Name').AsString := Request.ContentFields.Values['speciesname'];
    except err.Add('Error in ''Species Name'''); end;
    try Table1.FieldByName('Length (cm)').AsFloat   := StrToFloat(Request.ContentFields.Values['lengthcm']);
    except err.Add('Error in ''Length (cm)'''); end;
    try Table1.FieldByName('Length_In').AsFloat     := StrToFloat(Request.ContentFields.Values['lengthin']);
    except err.Add('Error in ''Length_In'''); end;
    try Table1.FieldByName('Notes').AsString        := Request.ContentFields.Values['notes'];
    except err.Add('Error in ''Notes'''); end;

    // Check for errors
    if err.Count = 0 then
    begin
      Table1.Post;
      Response.Content := InsertImageForm.Content;
    end
    else begin
      // Create the error output
      Response.Content := '';
      for i := 0 to err.Count - 1 do
        Response.Content := Response.Content + '<p>' + err.Strings[i] + '</p>';
    end;
    err.Free;
  end

  // If the 'New' button was pressed, Insert/Append the record
  else if b = 'New' then
  begin
    // Sends an empty form
    Response.Content := NewRecordForm.Content;
  end

  // If the 'Delete' button was pressed, Delete the record
  else if b = 'Delete' then
  begin
    if Table1.Locate('Species No', Request.ContentFields.Values['record'], []) then
    begin
      Table1.Delete;
      Response.Content := 'Record deleted';
    end
    else
      Response.Content := 'Unknown record';
  end
end;


// This method substitutes tags in the html-template stored in
// DataSetPageProducer1.HTMLDoc. Only the tags that could not be revolved
// automatically (due to spaces in the fieldnames of the table) will be
// substituted here.
// The 'Note'-field must be handled this way too.  
procedure TWebModule1.DataSetPageProducer1HTMLTag(Sender: TObject;
  Tag: TTag; const TagString: String; TagParams: TStrings;
  var ReplaceText: String);
begin
  if TagString = 'SpeciesNo' then
    ReplaceText := Table1.FieldByName('Species No').AsString
  else if TagString = 'SpeciesName' then
    ReplaceText := Table1.FieldByName('Species Name').AsString
  else if TagString = 'Length_cm' then
    ReplaceText := Table1.FieldByName('Length (cm)').AsString  
  else if TagString = 'Notes' then
    ReplaceText := Table1.FieldByName('Notes').AsString;
end;

procedure TWebModule1.WebModule1WebActionItem5Action(Sender: TObject;
  Request: TWebRequest; Response: TWebResponse; var Handled: Boolean);
const ENCTYPE = 'image/pjpeg';
var index: Integer;
    p: PChar;
    ms: TMemoryStream;
    j: TJPEGImage;
    bmp: TBitmap;
begin
// Insert the image in the record
//
// This piece of code is a hack since the 'multipart/form-data' encode-type
// is difficult to parse. It works on Internet Explorer 5 but may fail using
// other browsers.
  Table1.Locate('Species No', Request.QueryFields.Values['record'], []);
  index := Pos(ENCTYPE, Request.Content);
  if index = 0 then
    Response.Content := '<h2>No JPEG picture received</h2>' +
                        InsertImageForm.Content
  else begin
    try
      p := @Request.Content[index + Length(ENCTYPE) + 4];
      ms := TMemoryStream.Create;
      ms.Write(p^, Length(Request.Content) - index + 1);
      ms.Position := 0;
      j := TJPEGImage.Create;
      j.LoadFromStream(ms);
      bmp := TBitmap.Create;
      bmp.Assign(j);
      Table1.Edit;
      Table1.FieldByName('Graphic').Assign(bmp);
      Table1.Post;
      bmp.Free;
      j.Free;
      ms.Free;
      Response.Content := '<h2>Image has been stored</h2>';
    except
      Response.Content := '<h2>Fatal error</h2>';
    end;
  end;
end;
0
 
ARADTechCommented:
Yes I too would like to know how to send the post command to a browser window?  
0
 
rwilson032697Commented:
Listening
0
All Courses

From novice to tech pro — start learning today.