?
Solved

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

Posted on 2004-08-18
5
Medium Priority
?
2,343 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 375 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 375 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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A lot of questions regard threads in Delphi.   One of the more specific questions is how to show progress of the thread.   Updating a progressbar from inside a thread is a mistake. A solution to this would be to send a synchronized message to the…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
If you’ve ever visited a web page and noticed a cool font that you really liked the look of, but couldn’t figure out which font it was so that you could use it for your own work, then this video is for you! In this Micro Tutorial, you'll learn yo…
This is my first video review of Microsoft Bookings, I will be doing a part two with a bit more information, but wanted to get this out to you folks.
Suggested Courses
Course of the Month9 days, 21 hours left to enroll

762 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