简体   繁体   中英

How to Redirect or send some variable while submitting form from mysite (xyz.com) to other site (abc.com)?

I want to pass one variable to other site from my site while submitting form in my site.

Please help me.

Thanks in advance.

Kanji

If the form is submitted by POST, you cannot redirect the user. HTTP redirections cannot be made to contain POST variables that were submitted to the original receiving server.

Sorry!

PS - Consider using the destination site's APIs if they have them!

Add to HTML to form

<input type="hidden" name="ParamName" value="ParamValue" />

When checking form at target site using PHP get this variable using $_POST['ParamName'] like:

if (isset($_POST['ParamName'])) {
    echo 'I\'ve got ParamName - '.$_POST['ParamName'];
}

You can use AJAX to submit the form to your site and when completed use javascript to fire the submit event on the form itself.

Using jQuery for instance you can call

$("#myform").trigger("submit");

Would probably work on all modern browsers.

Making sure that I understand-

do you want one server to receive the results of your forum while you get redirected to a separate server?

The only practical reason I could imagine for that setup would be a phishing scam, but assuming you have honorable intentions in mind, you have a few options.

Submit the form to abc.com, then have abc.com process the variables and send a redirect header to send the user back to xyz.com. This method requires you own web space on both abc.com and xyz.com for creating the form and for processing the form.

xyz.com:
<form action="abc.com/..." method="get">
    <input type="text" name="value" />
    <input type="submit" />
</form>

abc.com:
<?php
    /* Do something with $_GET["value"] */
    header("Location: xyz.com");
?>

Use AJAX to submit the form contents to abc.com while submitting the form to xyz.com (I'm not comfortable enough with javascript to provide the source for this)

Have xyz.com submit the values to abc.com

<?php
    $output = file_get_contents("abc.com?value=".$_GET["value"]);
?>

You could also create a stream_context to send the POST to another server.

<?php
$context = stream_context_create(array( 
'http' => array( 
    'method'  => 'POST',
    'header'  => "Content-type: application/x-www-form-urlencoded\r\n",
    'content' => http_build_query(array('value' => $value)),
    'timeout' => 90,
    ), 
));
$ret = file_get_contents('abc.com', false, $context);
?>

Or, a 307 redirect (temporary redirect) should send POST data.

<?php
header("HTTP/1.0 307 Temporary Redirect", $replace=true, 307);
header("Location: xyz.com");
?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM