Solved

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

Posted on 2004-08-18
5
2,249 Views
Last Modified: 2008-10-10
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
Comment
Question by:JDJVR
  • 3
5 Comments
 

Author Comment

by:JDJVR
ID: 11838662
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
 
LVL 4

Assisted Solution

by:ceoworks
ceoworks earned 125 total points
ID: 11841032
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
 

Author Comment

by:JDJVR
ID: 11842178
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
 
LVL 4

Accepted Solution

by:
ErikPhilips earned 125 total points
ID: 11863214
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
 

Author Comment

by:JDJVR
ID: 11870217
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

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction Raise your hands if you were as upset with FireMonkey as I was when I discovered that there was no TListview.  I use TListView in almost all of my applications I've written, and I was not going to compromise by resorting to TStringGrid…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

758 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now