How does PHP submit form forms across domains? This article introduces 2 methods

Source: Internet
Author: User
Tags curl php code

Let's take a look at two ways to cross-domain submit form for PHP

one, through the PHP curl

function Curlpost ($url, $params)
{
$postData = ';
foreach ($params as $k => $v)
{
$postData. = $k. ' = '. $v. ' & ';
}
RTrim ($postData, ' & ');
$ch = Curl_init ();
curl_setopt ($ch, Curlopt_url, $url);
curl_setopt ($ch, curlopt_returntransfer,true);

curl_setopt ($ch, Curlopt_header, false);
curl_setopt ($ch, Curlopt_post, Count ($postData));
curl_setopt ($ch, Curlopt_postfields, $postData);

$output =curl_exec ($ch);

Curl_close ($ch);
return $output;
}

Echo curlpost ("http://test.com", Array (' name ' => "Tank"));

Before many people use curl to grasp, the mailbox address Book, but now is not OK. Ha ha.

two, using jquery Form,ajax to submit

1, download Jquery.form.js

2,js Code

$ (' #testform '). Submit (function () {
$ (this). Ajaxsubmit ({
Type: ' Post ',//Submit method Get/post
DataType: "JSON",//data type
URL: ' Your URL ',//URL to submit
Success:function (data) {//File returns after submission, typically JSON data
Data can be related to processing here
Alert (' Commit success! ');
}
$ (this). Resetform (); Reset form after submission
});
return false; Prevent forms from automatically submitting events
});

3,php Code

Header ("access-control-allow-origin:*"); Cross-domain permission settings that allow all

Header ("access-control-allow-origin:http://www.test.com"); Only allow test.com to submit data across domains

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.