Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

VFP -> post data to CGIscript to pre-populate a form

Status
Not open for further replies.

operaguy

Programmer
Aug 19, 2002
28
US
I want to do something simple: post data from VFP to a cgi script which generates a webpage with a Form on it, and have that form filled in already.

In otherwords, I want pre-populate a form on a webpage from VFP. That webpage is built on the fly by a cgi script.

I am on VFP 7.0
I have but I can't seem to figure out how to implement this idea with it.

I have the cgi script all set up and working --- including populating fields --- IF you post to it from an HTML form (action POST). See it here:

The CGI Script writes out the passed data to a variable, then defaults it into the input lines being built, using this code:

$first=$query->param('first');
$last=$query->param('last');
print<<EOF;
<BR>First name (hopefully defaulted from prior html page): <input type="text" name="First" value="$first">
<BR>Last name (hopefully defaulted from prior html page): <input type="text" name="Last" value="$last">


Now all I have to do is figure out how to post the data/name pairs from VFP to the cgi script

Any help gratefully appreciated, thanks
John Donohue
 
the comment(s) should read:
(hopefully defaulted from VFP):
 
If you can use GET instead of POST, here's an easy way:


just use it like this:
Code:
lcRes = ReadUrl('[URL unfurl="true"]https://ns11.reliablehosting.com/pauldonohue/cgi-bin/don5.cgi'[/URL] ;
+'?amount=55.99&first=First+Name&last=Last+Name' )
It all gets more complicated when you need to "POST" the form data, but many CGI's can deal with a GET or a POST.

If you really need to POST, notice inside ReadUrl, the comments:
Code:
    lnRet = HttpSendRequest( lhUrlFile, 0, 0, 0, 0 )
    *!*	        hRequest,                     // request handle
    *!*	        "",                           // header string
    *!*	        0,                            // header length
    *!*	        NULL,                         // post data
    *!*	        0                             // post length

Notice the NULL for POST data, and 0 for Post Length.

Modify the ReadUrl procedure to take a parameter for POST data:
Code:
LPARAMETERS pcUrlName, pcOptFeedback, pnOptBuffSize, pcOptOutputFile, pcOptUser, pcOptPwd, pcOptPOSTdata
then change the HttpSendRequest definition to:
Code:
DECLARE INTEGER HttpSendRequest IN wininet; 
    INTEGER hRequest,; 
    INTEGER lpszHeaders,; 
    INTEGER dwHeadersLength,; 
    STRING @ lpOptional,; 
    INTEGER dwOptionalLength
and pass the post data like this:

Code:
    lnRet = HttpSendRequest( lhUrlFile, 0, 0, @pcOptPOSTdata, len(pcOptPOSTdata) )

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
I've got to use post...the three simple fields in my example are just for testing...much more will actually be passed.

I am reading and attempting to absorb the rest of your reply about POST. Thank you.

John
 
Bill...I am going to ask for a little more. I have to preface this with the fact that I am a very un-experienced VFP programmer, and I can't grasp exactly how to construct the entire process of posting according to your suggestion. For instance, I don' understand where the program would list or loopFor the name/value pairs.

Is it possible for you to write out a full set of code (for POST, not GET) from top to bottom? I would not even know where to start, i guess it would be something like

DECLARE INTEGER HttpSendRequest IN wininet;
DO PostIt with "
PROCEDURE PostIt
LPARAMETERS pcUrlName, pcOptFeedback, pnOptBuffSize, pcOptOutputFile, pcOptUser, pcOptPwd, pcOptPOSTdata

INTEGER hRequest,;
INTEGER lpszHeaders,;
INTEGER dwHeadersLength,;
STRING @ lpOptional,;
INTEGER dwOptionalLength

lnRet = HttpSendRequest( lhUrlFile, 0, 0, @pcOptPOSTdata, len(pcOptPOSTdata) )

RETURN ""


-------------
but that certainly is not right
 
The POST values are the same as when they are in the query string in the URL... like this: amount=55.99&first=First+Name&last=Last+Name

The names and values are both URL-encoded (so that embedded special characters like "&" and "=" don't screw up the meaning of the the real "&" and "="s... see the URLEncode method here: and RFC documentation here: )

Here's an adjusted ReadUrl procedure to support POST:


- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
You can use it like this:
Code:
* pcUrlName, 
* pcOptFeedback, pnOptBuffSize, pcOptOutputFile, pcOptUser, pcOptPwd, 
* pcOptPOSTdata
Result = ReadUrl( "[URL unfurl="true"]https://ns11.reliablehosting.com/pauldonohue/cgi-bin/don5.cgi",[/URL] ;
.f.,.f.,.f.,.f.,.f.,;
'amount=55.99&first=First+Name&last=Last+Name' )

The other parameters (.f. above) are optionally used for showing progress feedback, sendign the output to a file (if it is bigger than the 16MB limit on strings), and supplying an HTTP AUTH user and password.

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Thanks for responding....

I tried this code exactly as suggested...


* pcUrlName,
* pcOptFeedback, pnOptBuffSize, pcOptOutputFile, pcOptUser, pcOptPwd,
* pcOptPOSTdata
Result = ReadUrl( " ;
.f.,.f.,.f.,.f.,.f.,;
'amount=55.99&first=First+Name&last=Last+Name' )

and got this error:

File 'ReadUrl.prg' does not exist.

Is there something I have to execute prior to the Result line?

J.
 
correction.....

i went to the link you provided, made a program readurl.prg and saved it into my VFP directory....

now the program finds it....

working with it now...

j.
 
Executing now, no error, (it is obviously finding the ReadUrl.prg but....

the browser window does not automatically open after post.

so, i added a few "navigate to" lines to try to get it to open....and it does so, but no values in the fields.

Is ReadURL.prg supposed to POST AND open the browser window? Or do I need somthing else?

here is the current code:

* pcUrlName,
* pcOptFeedback, pnOptBuffSize, pcOptOutputFile, pcOptUser, pcOptPwd,
* pcOptPOSTdata
Result = ReadUrl( " ;
.f.,.f.,.f.,.f.,.f.,;
'amount=55.99&first="Amy"&last="Jones"' )

LOCAL loHyperlink
loHyperlink = CREATEOBJECT("hyperlink")
loHyperlink.navigateto("
 
Here is one way of done it.

Code:
LOCAL tcUrl 
tcUrl= "[URL unfurl="true"]https://ns11.reliablehosting.com/pauldonohue/cgi-bin/don5.cgi?amount=100,000.00&first=Mike&last=Gagnon"[/URL]
oBrowser = CREATEOBJECT("internetexplorer.application")
oBrowser.navigate(tcURL)
oBrowser.visible = .t.

Mike Gagnon

If you want to get the best response to a question, please check out FAQ184-2483 first.
ReFox XI (www.mcrgsoftware.com)
 
Hi Operaguy,

I had tried to send this response last week, but Tek-Tips was down, then I was away... but here it is now:

The value of "Result" that is returned from ReadUrl IS the HTML webpage. You can write that HTML to a file, then point a browser to that file.

Do you really want the result to open in a browser window?
Usually that only happens because of not having any other choice.

However, if that IS what you want, I'd take a different approach:
1) Use VFP to create an HTML form in a temp location.
2) Launch that form into a browser
3) have Javascript in the form automatically Submit the form via POST...

Code:
lcFile = ADDBS(GETENV('TEMP'))+SYS(2015)+'.htm'
lcAmt   = '10.50'
lcFirst = 'First Name'
lcLast  = 'Last Name'

SET TEXTMERGE TO (lcFile) ON NOSHOW
\\<HTML>
\<BODY>
\<form NAME="myForm" ID="myForm" action="[URL unfurl="true"]https://ns11.reliablehosting.com/pauldonohue/cgi-bin/don5.cgi"[/URL] method="POST">
\Amount: <input type="textbox" maxlength="15" name="amount" value="<<lcAmt>>"> 
\<BR>
\First name: <input type="text" maxlength="40" name="first" value="<<lcFirst>>">
\<BR>
\Last name: <input type="text" maxlength="40" name="last" value="<<lcLast>>">
\<BR>
\<input type="submit" name="SubmitForm" value="Submit">
\</form>
\<script language="JavaScript">
\document.myForm.submit();
\</script>
\</BODY>
\</HTML>
SET TEXTMERGE TO 

lnRes = ShellExec( lcFile, SET('DEFAULT')+CURDIR(), 'OPEN', '' )

************************************************************************
* WinApi :: ShellExecute*********************************
***    Author: Rick Strahl, West Wind Technologies
***            [URL unfurl="true"]http://www.west-wind.com/[/URL]
***  Function: Opens a file in the application that it's
***            associated with.
***      Pass: lcFileName  -  Name of the file to open
***            lcWorkDir   -  Working directory
***            lcOperation -  "Open" "Run" "Play" "Edit" etc...
***    Return: 2  - Bad Association (invalid URL)
***            31 - No application association
***            29 - Failure to load application
***            30 - Application is busy***
***            Values over 32 indicate success
***            and return an instance handle for
***            the application started (the browser)
************************************************************************
FUNCTION ShellExec
LPARAMETERS lcFileName, lcWorkDir, lcOperation, pcParameters
LOCAL pp, lcParam
pp = pCount()          && LAS v9b1w  wgcs
if pp>3                    && LAS v9b1w  wgcs
  lcParam = pcParameters   && LAS v9b1w  wgcs
else                       && LAS v9b1w  wgcs
  lcParam = ''
endif
lcWorkDir=IIF(type("lcWorkDir")="C",lcWorkDir,"")
lcOperation=IIF(type("lcOperation")="C",lcOperation,"Open")
DECLARE INTEGER ShellExecute ;    
    IN SHELL32.DLL ;    
    INTEGER nWinHandle,;
    STRING cOperation,;    
    STRING cFileName,;    
    STRING cParameters,;
    STRING cDirectory,;    
    INTEGER nShowWindow
RETURN ShellExecute(0,lcOperation,lcFilename,lcParam,lcWorkDir,1)
ENDFUNC  ShellExecute

- Bill

Get the best answers to your questions -- See FAQ481-4875.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top