...

View Full Version : Help with form POST and redirect



Whoopage
02-27-2008, 10:11 PM
Hey all - new to the site and could really use some help.

My situation: I have a form that needs to post results to a specific URL (which is on a different server and I have no control over). Essentially it is a lead generating form...

But after I post to the URL, I want it then to redirect back to a page on my site, a thank you page.

I believe I need some sort of form processing script, where the form results are processed, sent to the URL and then the script redirects or displays a Thank you message.

I don't know how to write such a script though.

I know that the form action will need to call the script, but I don't know how to pass the results to the processing script nor how to make the script then POST them to the URL.

Any help would be greatly appreciated - Thanks!

meth
02-27-2008, 10:51 PM
Read the script comments and replace the variable values with your webpage urls:




<?php
//SCRIPT VARS
//url of ewbpage to submit POST array to
$curl_url = 'http://www.externalDomain.com/thisPage.php';
//thanks webpage
$redirect_success = 'success.php';
//error webpage
$redirect_failure = 'failure.php';

//submit POST array to external webpage
if(isset($_POST['Submit'])) {

$postVals = '';
foreach($_POST as $k => $v) {
$postVals .= "$k=".urlencode($v);
}

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $curl_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postVals);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

//here you would normally check for success or failure
//by searching $result for positive or negative response.
//for now, just redirect to the thanks page if $result is not empty
if(!empty($result)) $redirect = $redirect_success;
else $redirect = $redirect_failure;
header("Location:$redirect");

}
?>

meth
02-27-2008, 10:58 PM
Hmm, thought I might add that the form on your webpage submits to its self (self submitting form) and the submit button should be named "Submit".

Whoopage
02-27-2008, 11:58 PM
Thank you for your posts - I think this is getting me where I need to be.

So what should the 'action' look like for a self submitting form? Or what should the whole <form...> stuff look like when self submitting? (for reference I will call the file with the code you gave me processing.php)

Again - thank you very much for you help

meth
02-28-2008, 12:24 AM
action="?" will do the trick

Whoopage
02-28-2008, 12:32 AM
Thanks - where then do I refer to the file that contains the code you gave me?

Or does that code go on the same page as the form?

Also - for reference - my form:



<form method="post" name="debtform" onSubmit="return isValid(document.debtform)" action="?">

/*form stuffs*/

<input name="Submit" type="image" src="images/continue.jpg"/>
</form>

Ultragames
02-28-2008, 01:34 AM
I would suggest using the CURL code posted above, but on a seperate page, and then redirecting back to your form page. Otherwise, if the user hits that page again, whether through a refresh, or the back/forward buttons, that POST data will be resent. Which is probably not what you want.

meth
02-28-2008, 02:02 AM
Ultragames: If the user submits the form, they are redirected to a stateless success/error page; refresh will have no effect. I'll code in nocache headers eliminate the resend.

Whoopage: the final product will look like this:




<?php
// no using the back button !!
header("Expires: Mon, 26 Jul 1997 05:00:00 GMT");
// always modified
header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT");
// HTTP/1.1
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Cache-Control: post-check=0, pre-check=0", false);
// HTTP/1.0
header("Pragma: no-cache");

//SCRIPT VARS
//url of ewbpage to submit POST array to
$curl_url = 'http://www.externalDomain.com/thisPage.php';
//thanks webpage
$redirect_success = 'success.php';
//error webpage
$redirect_failure = 'failure.php';

//submit POST array to external webpage
if(isset($_POST['Submit'])) {

$_SESSION['submit_form'] = true;
$postVals = '';
foreach($_POST as $k => $v) {
$postVals .= "$k=".urlencode($v);
}

$ch = curl_init();
curl_setopt ($ch, CURLOPT_URL, $curl_url);
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $postVals);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($ch);
curl_close($ch);

//here you would normally check for success or failure
//by searching $result for positive or negative response.
//for now, just redirect to the thanks page if $result is not empty
if(!empty($result)) $redirect = $redirect_success;
else $redirect = $redirect_failure;
header("Location:$redirect");

}
?>
<form method="post" name="debtform" onSubmit="return isValid(document.debtform)" action="?">

<!-- form stuff -->

<input name="Submit" type="image" src="images/continue.jpg"/>
</form>

Whoopage
02-28-2008, 04:52 AM
I placed the php code on the same page as the form, above all the html code (all the way up above the opening <html>). I changed the variables to direct to the outside website, and the correct error and success page, made sure that the form referred to '?' - submitted and...

The same page reloaded, and the only difference was a '?' on the end of the URL.

I didn't mention earlier, and this may be very important - that the page I am supposed to post the results of the form to is (and this is obviously not the whole address, just the end) '...public/LeadImport.asmx/LeadReceiver'

I don't know if that means PHP will have issues or won't work with it - but it did not seem to work when submit was hit. The page refreshed, but no thankyou page, and no lead ever made it through.

I hate to keep coming back for help - I know I'm out of my league here, but I really do appreciate all the help from everyone.

If I need to give more info, please let me know - if you need to see the site, tell me and I'll PM you.

Thanks again everyone.

Whoopage
02-29-2008, 05:46 AM
Would it matter that I'm sending to a .asmx page?

Whoopage
03-03-2008, 09:47 PM
I'm trying to post the form results to an asmx page - does that matter? Will that make curl not work?

I tried the above code and I get nothing. Could desperately use some help here.

I want to post to a third party lead collector, and they have given me the following URL to do so. It looks something like this:

http://www.example.com/.../LeadImport.asmx/LeadReceiver

Everything works fine when I use that URL in the action of the form, and everything is posted through - they get the lead. The problem is, doing it this way the user is redirected to the above URL and away from my site - and they see the receiving page and that will cause confusion.

What I need to do is have a form processing script that will take the post results from the form, post to the above URL, but redirect the user to a ThankYou page on my site.

Whoopage
03-03-2008, 11:25 PM
I seem to have figured it out with the following code:


<?php

//thanks webpage
$redirect_success = '/thankyou.php';
//error webpage
$redirect_failure = '/failure.php';

$ch = curl_init();

// Follow any Location headers
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

curl_setopt($ch, CURLOPT_URL, 'http://www.leadproweb.com/.../LeadImport.asmx/LeadReceiver');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

// Alert cURL to the fact that we're doing a POST
curl_setopt($ch, CURLOPT_POST, 1);

curl_setopt($ch, CURLOPT_POSTFIELDS, "FirstName=$_POST[FirstName]&LastName=$_POST[LastName]&Email=$_POST[Email]&State=$_POST[State]&Zip=$_POST[Zip]&PrimaryPhone=$_POST[PrimaryPhone]&BestTime=$_POST[BestTime]&UnsecureDebt=$_POST[UnsecureDebt]&PmtStatus=$_POST[PmtStatus]&HomeOwner=$_POST[HomeOwner]&VID=##&AID=##&LID=##&IPAddress=$_POST[IPAddress]");

$output = curl_exec($ch);
curl_close($ch);

$redirect = $redirect_success;

header("Location:$redirect");

?>

Thanks for all your help everyone!



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum