Passing data to “thank you” page when form action

2019-07-21 14:16发布

I have a web form that collects a user's zip code as one of the field and posts to a third party site...

<form name = "foo" action="https://thirdpartysite.com" method="post">

The form has a "redirect_link" field that tells the third party site to redirect to my thank you page, so the form processing happens on their side. If I were doing the form processing, I could store the zip code in a session variable before saving it to my database, and pass it to the thank you page in that way. But because the form processing is being done by someone else, I'm not sure how I can access that zip code field, store it somewhere and pass it to my thank you page. I'm thinking maybe I could do it by using javascript to grab the zip code and storing it in a php session variable? I'm not sure if that would work, or how to do it.

标签: php forms
1条回答
SAY GOODBYE
2楼-- · 2019-07-21 14:58

If the form has a "redirect_link" field, couldn't you just include it there as a GET parameter?

<input value="http://www.somesite.com/thankyou.php?zip=12345" type="hidden" name="redirect_link"  />

You would need to add JavaScript to the zip code input so that when it updates, it also updates the hidden field.

As you mention, another option would be to use PHP sessions, but since it would also require sessions, you're not going to gain anything over doing it in a hidden field (e.g. if people have session cookies disabled).

A third option I could think of, if it's possible, would be to use your own form to submit the data to the third party site using cURL. This would obviously only work if the user does not need to do anything on the remote site:

$url = 'http://www.othersite.com/script.php';
$data = array(
    'name' => $name,
    'email' => $email,
    ...
);

//set the url, number of POST vars, POST data
curl_setopt($ch,CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_POST, count($data));
curl_setopt($ch,CURLOPT_POSTFIELDS, http_build_query($data));

//execute post
$result = curl_exec($ch);

//close connection
curl_close($ch);

And finally, quite often sites that will redirect the user back to your site will have the ability to automatically append certain things to the return URL or send POST data to it. Check with the API of the site you are using has this ability. If so, this would probably be the easiest and best way to do it.

查看更多
登录 后发表回答