Hi please help, I have a simple web form and when the submit button is pressed I would like the form to post to two locations.

One location is local and the other is external.

Hope you can help.

Thanks

Recommended Answers

All 19 Replies

Hi,
I unfortunately don't have time to find an exact reference, but PHP has something called cURL - which can do a variety of things, but Im sure it can post vars to a URL and because it doesn't require actual navigation to that URL, you could catch your form inputs and then just do two (or more) cURL requests...

Check out the php.net manual, or just google 'php cURL post'

Hope it works out,

lworks

I found this code..
test at your end.

function postit($host, $url, $postdata) {
$fp = pfsockopen ( $host, 80, &$errno, &$errstr, 60 );
if( ! $fp ) return "$errstr ($errno)<br>\n";
fputs ($fp,"POST $url HTTP/1.1\n");
fputs ($fp,"Host: $host\n");
fputs ($fp,"User-Agent: Autopost demonstration script\n");
fputs ($fp,"Accept: */*\n");
fputs ($fp,"Content-type: application/x-www-form-urlencoded\n");
fputs ($fp,"Content-length: ".strlen($postdata)."\n\n");
fputs ($fp,"$postdata\n\n");
$output = "";
while( !feof( $fp ) ) {
$output .= fgets( $fp, 1024);
}
fclose ( $fp);
return ($output);
}

$host = "www.abc.com";
$url = "page.php";
$postdata = "name=john";
postit($host, $url, $postdata);

Another way to do it is you push your data to one file. Then write what you need the internal file and the rest just push to the other.

Hi jrock, sorry not sure how to push from the second file? How would you take the data and send again?

Thanks

Well is the second file a php file or is it going to be some other filetype?

Hi, yes you are correct it is a PHP file.

I would like to try and revive this question.

I have an html for and I need to post all fields of the form to two urls, one is PHP and the other is external.

Can anyone help me or point me in the right direction.

Thanks in advance

Sorry it took me so long. What I would do is on that php file I would use jQuery that grabs the variables and then sends them to the other file. This external file what does it do? Is it a text file?

I don't think I'm getting this correctly, but anyway, something like this.

$content=$_POST['text'];
file_put_contents("http://external.cc/a.txt", $content . ";", FILE_APPEND);
file_put_contents("b.txt", $content . ";", FILE_APPEND);

Since you're talking about external, I'm thinking you don't mean you want to have this

file_put_contents("http://external.cc/a.txt", $content . ";", FILE_APPEND);

be allowed in php.ini, so you would probably just have one file on the local website. Alternatively, you could do this:

$content=$_POST['text'];
file_put_contents("a.txt", $content . ";", FILE_APPEND);
//the same as before until now
echo "<script>window.location=\"http://external.cc/a.php\"</script>"

and then http://external.cc/a.php (the external website)

$content=file_get_contents("http://local.us/a.php"); //the "local" website
file_put_contents("a.txt", $content . ";", FILE_APPEND);
Member Avatar for diafol

If your formpage is local:

send form to local page - this will deal with the data, but then send on the $_POST data like the code below.

$postdata = http_build_query(
    array(
        'var1' => $_POST['var1'],
        'var2' => $_POST['var1']
    )
);

$opts = array('http' =>
    array(
        'method'  => 'POST',
        'header'  => 'Content-type: application/x-www-form-urlencoded',
        'content' => $postdata
    )
);

$context  = stream_context_create($opts);

$result = file_get_contents('http://example.com/submit.php', false, $context);

This was taken from the php manual. Haven't tried it.

BTW - don't know how you'd get data to your localhost if your form is on remote server.

Hi jrock2004

the external file is an asp file, it processes the fields I send, I don't have to do anything with that file other thank POST the data to it.

Thanks

Member Avatar for diafol

Post data sent via form is essentially html-driven through http header. I can't imaging that sending data from a php form page to an asp page would be a problem.

THis is where I'm getting confused. Are you saying that you've got both php and asp pages on your localhost server? I can't see that working, but perhaps that's just me. The local file would have to do the processing to send the data to the remote file (on remote server). ARe you sure you're looking for a php solution or do you need an asp solution?

Hi thanks for responding, the PHP page is local and the asp page is remote (on another server, different domain) but I only need to POST to the asp page, nothing else happens with that URL.

does this make better sense

Member Avatar for diafol

Can't see the problem then. Just post the form to the asp page. You can post forms to other domains. You can send post data locally which can then send a form to your asp page by programmatically (js) clicking a form button on a hidden form. few ways to do it.

Hi Ardav, yes your solution seems very logical, can you enlighten me as to the best way to:

"clicking a form button on a hidden form. few ways to do it."

As I a sure you can tell I am really struggling with posting this data to the second URL after the first.

Nic

Member Avatar for diafol

OK, so you post locally first - to a php or an asp file?
Then the data gets sent on to a remote page - php or asp?

I need to know the order and the file type. Is it like this:

form page (local) -> receive page (local) -> receive page (remote)

Your local receive page will be the crucial element.

You can populate the a form on the receive page and programmatically send that second form to the remote:

document.forms['formname'].elements['send2'].click() //or perhaps the

document.forms['formname'].submit(); //would work

..form.. filled with $_POST data from the first form... 
<input name="send2" id="send2" type="submit" />

//EDIT
Listen nic, I'm sure there must be a much easier way of doing this, but I can't think of it. Anybody else?

Yeah I am sure there must be....

The order is, form page >> php (local) >> asp (remote)

Logically it is one form with two actions....

Driving me crazy....

Member Avatar for diafol

OK, I think I got a solution for you, along the lines I mentioned. Here's a mockup:

Page1 (local) PHP OR ASP OR HTML
================================

<form method="post" name="try1" id="try1" action="receive1.php">
<input name="data" id="data" value="any old text here" />
<input type="submit" name="send" id="send" value="Send" />
</form>

Receiver (local) PHP
====================

...(do any server side processing you need, e.g. error checking etc)...
<form method="post" name="try2" id="try2" action="http://www.example.com/handler.asp">
<input name="data2" id="data2" value="<?php echo $_POST['data'];?>" />
</form>
<script>document.forms['try2'].submit();</script>

Receiver (remote) ASP
======================
I don't do ASP, but the $_POST equivalent for the 'data2' field can be picked up here.

I would use code like this in biterscripting.

Assume the two pages to post are "http://www.site1.com/post.asp" and "http://www.site2.com/post.asp".


Here is the code

# Script Post2.txt
# Start two sessions.
isstart s1 "site1" "Mozilla/4.0" ; isstart s2 "site2" "Mozilla/4.0"
# Connect sessions to site1 and site2.
iscon s1 "http://www.site1.com" ; iscon s2 "http://www.site2.com"
# Post forms. 
ispost s1 "post.asp" "fieldname=fieldvalue"  ; ispost s2 "post.asp" "fieldname=fieldvalue"
# Disconnect and end sessions.
isdiscon s1 ; isdiscon s2
isend s1 ; isend s2

Can do more fancy work with internet sessions. Details at http://www.biterscripting.com/helppages_automatedinternet.html .

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.