[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2381
  • Last Modified:

TidHTTP component sample code to pass variables between a Delphi app and PHP form on the Web

Hello folks,

EddieShipman put me on the right track with a problem I had last week by directing me towards using PHP as part of the solution. Now I have hit a snag with the TIdHTTP component usage, which I know very little about. I need some example code (Delphi) to pass a value between a Delphi app and a PHP form, but this value is unknown to the user, so it must find the value of the Delphi variable first and then pass it HIDDEN FROM SCRUTINY. Let's say the value to be passed is a string, and the variable name is 'Prod_Num' in the Delphi code. I click a button 'btnRegister' to submit the value to the PHP script, called 'form.php', located in "http://localhost/". Let's assume futher that the value will be received into the variable "$user_code" on the PHP side. Using these variable and component names (and TIdHTTP), could someone
give me some example code for the OnClick event of btnRegister? Please use the exact names as described because I am somewhat confused at the moment due to insomnia having extensively studied PHP and other new stuff (new to me, at least) for the last few days.

EddieShipman?, anybody?

Thanks
JDJVR
0
JDJVR
Asked:
JDJVR
  • 3
2 Solutions
 
JDJVRAuthor Commented:
Hmmm....I see nobody has ventured an attempt to reply to this question in almost a day. I am going to up the points to 250 - I doubt the question is a difficult one for the experts, but I could be wrong. Come on, folks, help me out.
Regards
JDJVR
0
 
ceoworksCommented:
Hi, not that much hard as you guessed :)

--- Delphi Side ---

procedure TForm1.btnLoginClick(Sender: TObject);
var
  SL: TStringList;
  i: integer;
begin
  SL := TSTringList.Create;
  try
    SL.Values['username'] := 'JDJVR';
    SL.Values['password'] := 'mypass19';
    SL.Text := Http.Post(http://localhost/logintest.php, SL);
 
    if (SL.Text = 'OK') then
    begin
      Caption := 'User authorized..';
    end
    else begin
      Caption := 'User could not authorized';
    end;
  finally
    SL.Free;
  end;
end;

--- PHP Side ---

(i) check the username and password :

  $username = $_POST[username];
  $password = $_POST[password];

(ii) return the result with echo or print function

HTH,

Oktay Sancak
0
 
JDJVRAuthor Commented:
Thanks, ceoworks for the attempt, but it does not answer my question. I need to get a grip on the actual syntax of the idHTTP usage here. Although I can follow the logic you propose, your example differs fundamentally from what I ask in my question. Your example shows how values YOU INSERTED YOURSELF into the stringlist, is passed to the PHP form. If you read my original question carefully, however, you will notice that the VALUE of the variable that is passed to the PHP form is NOT KNOWN TO THE PROGRAMMER because it is generated by the Delphi app at runtime. I am not sure whether a simple reference to the Delphi variable name in the idHTTP component will work (I think not). Whoever tackles this question - could you try to use the EXACT example I proposed in my original question? I kept it short so as not to waste anybody's time, and it would eliminate any ambiguity. (All I really need to see is the syntax of how a simple Delphi variable (string, integer, whatever) is passed to a PHP script without knowing its value at designtime.)

Regards
JDJVR
0
 
ErikPhilipsCommented:
I know pretty much nothing about PHP, but I will attempt to decode ceoworks "work" (with slight modifications):

//This should be easy, this is the event for the button, when its clicked
procedure TForm1.btnLoginClick(Sender: TObject);
var  // variable declaration
  SL: TStringList;   // StringList variable, a more complex version of
begin
  SL := TSTringList.Create;  //Create the string list which will contain a <variable_Name>=<variable_Value> passed to a website
  try
    // I've never added values to a stringList like this, but I'll assume it works
    SL.Values['user_code'] := Prod_Num;  // OK create a Variable named user_code we will pass to PHP with the value of the DELPHI variable prod_num
    SL.Text := Http.Post(http://localhost/logintest.php, SL);  //  This now sends a http request (type Post) with user_code=<value of Prod_Num>
 
    if (SL.Text = 'OK') then  //If the website return 'OK' meaning (i'm going to assume) that it was successful
    begin
      Caption := 'User authorized..';  // post was successful
    end
    else begin
      Caption := 'User could not authorized';  // post was unsuccessful
    end;
  finally  //This makes sure that if there are any errors in the above code, that we clean up after ourselves
    SL.Free;  // you *have* to free created objects that are not of type TComponent as they have no parent/ower the will clean them up
  end;
end;
0
 
JDJVRAuthor Commented:
Woe is me! The answer was staring me in the face and I didn't see it. (Shows you what lack of sleep can do to a person). Thanks ErikPhilips and also ceoworks for your contributions - I managed to slap together some code which worked after reviewing your answers.

Regards
JDJVR
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now