Forum Moderators: coopster & phranque

Message Too Old, No Replies

Sending postdata to another page

How is it done?

         

adni18

12:01 am on Mar 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi. How can sending postdata to another page, then redirecting to that page be done within perl?

coopster

2:25 am on Mar 6, 2005 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



You mean how do you maintain state? You can maintain state in a number of ways, such as cookies, <input type="hidden"> variables, sessions ... then the "action" of the <form> variable would be your next page.

adni18

2:31 am on Mar 6, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, no. I know all that. I mean, instead of hi.cgi?a=b&b=c, you could just do hi.cgi and the variables would not appear in the address bar.

Moby_Dim

3:53 pm on Mar 6, 2005 (gmt 0)

10+ Year Member



form method "post"

adni18

2:03 am on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



:o) Yeah. That's kind of obvious (to me). I was thinking more of a way directly thru perl. Ex:

you can send GET data like this:

print "Location:hello.cgi?text=hello+world\n\n";

but what if you don't want the?text=hello+world to appear?

Something like this, perhaps?

postdata("hello.cgi",text => "hello+world");

rocknbil

5:06 pm on Mar 8, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


$result = qq¦curl -d key1=value+1&key2=value+2 http://www.url_to_post_to.com¦;

print "content-type: text/html\n\n";
print "$result";

Curl is one way to post data silently. The -d switch means that the first param is data to be sent, second is the URL, what gets returned is stored in result. Depending on what the url sends back, this can be an entire page, a delimited string, or more key/value pairs.

PhraSEOlogy

8:01 pm on Mar 8, 2005 (gmt 0)

10+ Year Member



Rocknbill,

If I have a perl CGI script which I execute to post form data can I use curl to do it?

e.g. <a href="blah.cgi">BUY WIDGET</a>

and blah.cgi contains the form data to be posted

e.g.

[example.com...]

I tried the code you posted here but it only displays the curl command in the browser window and does not actually invoke the form destination URL.

adni18

2:24 am on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Same here; this doesn't work:

#!/usr/bin/perl

use CGI;

$result = qq¦curl -d q=value+1 [google.com...]

print "content-type: text/html\n\n";
print "$result";

Moby_Dim

9:09 am on Mar 9, 2005 (gmt 0)

10+ Year Member



OK. One of the possible solutions (not very elegant) is : a)script a.cgi writes data to >file.txt and b) send the user to script b.cgi, which c) reads the data from the same file.

rocknbil

8:00 pm on Mar 9, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



ARRGH!

Sorry Phraseology and adni, it doesn't work because I used qq and not qx, which executes.

But you guys/gals should have known that. :-) It was a test, yeah, that's it . . .

The reason I did that accidentially is I sometimes have very very long curl strings and it's easier to debug if I word-quote it first, then execute it:

$string = qq¦curl -d key1=value+1&key2=value+2 [url_to_post_to.com¦;...]
$result = qx¦$string¦;
##OR $result = `$string`;

print "content-type: text/html\n\n";
print "$result";

That should successfully store the RESULTS of what is curled in $result.

adni18

1:21 am on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Are those supposed to be A's with the little accent marks over them?

PhraSEOlogy

2:40 am on Mar 10, 2005 (gmt 0)

10+ Year Member



That should successfully store the RESULTS of what is curled in $result.

Rocknbill,

The results are returned to the perl script - but thats not what I want. I want the script to post the data and the results to be sent back to the browser.

I know I can simply print the $result but the browser will still point to site-A. Whereas if the user submits an html form on a page he is directed to site-B as the form is processed.

Or am I just muddying the waters even more?

rocknbil

6:44 pm on Mar 10, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adni,

AAARGH! No, I have no idea where those came from. This message board, apparently.
(tries again) :-D

$string = qq¦curl -d key1=value+1&key2=value+2 [url_to_post_to.com¦;...]
$result = qx¦$string¦;
##OR $result = `$string`;

I know I can simply print the $result but the browser will still point to site-A. Whereas if the user submits an html form on a page he is directed to site-B as the form is processed.

So why wouldn't this work?

$string = qq¦curl -d key1=value+1&key2=value+2 [url_to_post_to.com¦;...]
$result = qx¦$string¦;
## Do something with $result
print "Location: somwhere_else.html\n\n";

Or maybe you're looking for a forked process? Yah I think you might have lost me. :-)

PhraSEOlogy

6:07 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



Rocknbill,

Let me give you a real world example of what I am trying to do. I am going to use google as an example (I dont want to post specific urls here but I think using google as an example will be okay).

Site-A has a google search box form.
You enter a search term(s) and submit!
The browser is directed to site-B (google.com) and a list of results are displayed - voila!

What I want to do is...

Site-A has a link to a script on site-A.
Perl script on site-A simulates the form filling
process and submits form.
Then somehow pass control back to the browser so that the browser gets the results from site-B (even though the form was submitted by a script).

Maybe the answer is simple and I just dont see it.

rocknbil

6:32 pm on Mar 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Or you could do results from both.

print "welcome to site A\n";
print qq¦<form action="site_a.cgi">¦;
.....

## We don't need to curl for site A, it's right here.
$site_a_results = &yourSearchOutput;

$string = qq¦curl -d key1=value+1&key2=value+2 [site_b.com¦;...]
$site_b_results = `$string`;

print "here are results from site A:$site_a_results\n\n";

print "here are results from site B:$site_b_results\n\n";

? :-)

PhraSEOlogy

11:49 pm on Mar 11, 2005 (gmt 0)

10+ Year Member



I think this is going to make me NUTZ!

Just cant figure out how to articulate this problem - maybe I should just live with the html form!

Although, it may be nice to live in a room with padded walls and free food...

adni18

10:19 pm on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think what needs to be done is take the user to a script, with the variables posted in POSTDATA, not GETDATA. It should show the site with the variables inputted:

ex:
in the address bar: [google.com...]
hidden variables, contained in POSTDATA: q=this+is+a+search

INSTEAD OF:
in the address bar: [google.com...]

MichaelBluejay

11:38 pm on Mar 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I may.... I think the question might be, "How do I get Perl to click a Submit button (or at least submit a form the same way as if the user had clicked)?"

So far as I know, it can't be done. Javascript can do it, but not Perl, is my understanding.

adni18

1:34 am on Mar 23, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What I was trying to do was get perl to do the exact same thing that hitting "submit" on a POST METHOD form would do, only automatically (systematically).

PhraSEOlogy

3:08 pm on Mar 28, 2005 (gmt 0)

10+ Year Member



Adni,

there is a solution here:

[webmasterworld.com...]

MichaelBluejay

7:28 pm on Mar 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I'm not sure that would work. Say you wanted to submit a Buy Now form to PayPal. If you redirect to PayPal with a print Location without PayPal receiving any form data when the user arrives there, PayPal will give an error.