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 bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Submiitting Form to 2 URLs 2

Status
Not open for further replies.

audiopro

Programmer
Apr 1, 2004
3,165
GB
Is it possible to submit the same form to 2 URLs?
I set up a web form for a client which submits to a script and processes ok.
He now wants the same info also to be sent to an online contact manager. The contact manager script is supposed to send all of the posted data back to the calling script but it only returns the query string, not the form fields.

I wondered if the was possible to post the data to my original script, return a confirmation screen to the visitor and send the same data onto the contact manager script.


Keith
 
Hi

Keith said:
Is it possible to submit the same form to 2 URLs?
Yes, if the [tt]form[/tt] is not discarded after the first submit. You can achieve this in various ways :
[ul]
[li]Set the [tt]target[/tt] of the [tt]form[/tt] to an invisible [tt]iframe[/tt] so the server's response not replaces the current document.[/li]
[li]Modify the server-side script which handles the first request to return HTTP response code 204 No Content and send no content.[/li]
[li]Submit the [tt]form[/tt] data to the first URL using AJAX.[/li]
[/ul]
So one of the submits has to be done by JavaScript.

But while talking about this in the Perl forum, why not put your Perl script to send the received data forward to the second URL ? Is much stable than involving the user agent.

Feherke.
 
But while talking about this in the Perl forum, why not put your Perl script to send the received data forward to the second URL ? Is much stable than involving the user agent.
I am not sure I understand what you mean by that.
I have it working at the moment where the form is submitted to my own script, the contents are processed, emails are sent out and then the details are sent to the second url using an onload submit command. The 2nd url is set up to call the script back so I can return a thank you page to the visitor.
I was wondering how this could be done if the 2nd URL did not call my script.



Keith
 
Hi

Ok, now neither I understand all this. Especially the underlined part :
Keith said:
I have it working at the moment where the form is submitted to my own script, the contents are processed, emails are sent out and then the details are sent to the second url using an onload submit command.

Feherke.
 
Sorry - I didn't explain it very well.
It's the javascript HTML form submit command in the form of
Code:
print "<body onLoad='document.contact_man.submit();'>";


Keith
 
Hi

Got it. I did not even thought to such solution because it looks too bad for me.
What I suggested is :
[ul]
[li]"the contents are processed"[/li]
[li]"emails are sent out"[/li]
[li]make the request to the second URL from your Perl script, using the LWP module[/li]
[/ul]
Code:
[gray]#!/usb/bin/perl[/gray]

[b]use[/b] CGI[teal];[/teal]
[b]use[/b] LWP[teal]::[/teal]UserAgent[teal];[/teal]

[b]my[/b] [navy]$cgi[/navy][teal]=[/teal]CGI[teal]->[/teal]new[teal];[/teal]

[gray]# the contents are processed[/gray]
[gray]# ...[/gray]

[gray]# emails are sent out[/gray]
[gray]# ...[/gray]

[gray]# make the request to the second URL[/gray]
[b]my[/b] [navy]$ua[/navy][teal]=[/teal]LWP[teal]::[/teal]UserAgent[teal]->[/teal]new[teal];[/teal]
[b]my[/b] [navy]$req[/navy][teal]=[/teal]HTTP[teal]::[/teal]Request[teal]->[/teal][COLOR=darkgoldenrod]new[/color][teal]([/teal]POST[teal]=>[/teal][green][i]'[URL unfurl="true"]http://example.com/the_second_url/'[/URL][/i][/green][teal]);[/teal]
[navy]$req[/navy][teal]->[/teal][COLOR=darkgoldenrod]content_type[/color][teal]([/teal][green][i]'application/x-www-form-urlencoded'[/i][/green][teal]);[/teal]
[navy]$req[/navy][teal]->[/teal][COLOR=darkgoldenrod]content[/color][teal]([/teal]
  [green][i]'firstname='[/i][/green][teal].[/teal][navy]$cgi[/navy][teal]->[/teal][COLOR=darkgoldenrod]param[/color][teal]([/teal][green][i]'firstname'[/i][/green][teal]).[/teal][green][i]'&'[/i][/green][teal].[/teal]
  [green][i]'lastname='[/i][/green][teal].[/teal][navy]$cgi[/navy][teal]->[/teal][COLOR=darkgoldenrod]param[/color][teal]([/teal][green][i]'lastname'[/i][/green][teal])[/teal]
[teal]);[/teal]
[b]my[/b] [navy]$res[/navy][teal]=[/teal][navy]$ua[/navy][teal]->[/teal][COLOR=darkgoldenrod]request[/color][teal]([/teal][navy]$req[/navy][teal]);[/teal]
Note that I just kept it simple. You will have to URL encode the data when composing the request content.

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top