简体   繁体   English

将表单提交到另一个站点上的表单脚本的最佳方法是什么?

[英]What's the best way to submit a form to a form script on another site?

I am working on a basic HTML page that requires the user to send details to a script located on a third-party website. 我正在一个基本的HTML页面上工作,该页面要求用户将详细信息发送到位于第三方网站上的脚本。 What I require is for the user to fill out a form on my web page, and have that information submitted to another third-party form. 我需要的是用户在我的网页上填写一个表格,并将该信息提交给另一个第三方表格。

I do not wish to return anything to the user, other than whether the submission was successful or not. 除了提交成功与否,我不希望将任何东西退还给用户。 I also do not want the user to have to go to this third-party site to submit using their form. 我也不希望用户必须访问此第三方网站才能使用其表单进行提交。

It was suggested by the website itself to use an iframe and hold its form on your page, but I was wondering what other, preferably better methods are available to me. 网站本身建议使用iframe并将其表单保留在您的页面上,但我想知道还有哪些其他方法,最好是更好的方法。 It'd be nice if there were some form of jQuery/js code I could use to do such a thing. 如果我可以使用某种形式的jQuery / js代码来做这样的事情,那将是很好的。

It'd be nice if there were some form of jQuery/js code I could use to do such a thing. 如果我可以使用某种形式的jQuery / js代码来做这样的事情,那将是很好的。

One way is to use jQuery's $.ajax or $.post methods like this: 一种方法是使用jQuery的$.ajax$.post方法,如下所示:

$.ajax({
  url: url,
  success: function(data) {
    alert('succeeded');
  }
});

也许您可以使用CURLOPT_POST和CURLOPT_POSTFIELDS尝试使用cURL?

well it depends if you have control over the other website as well. 取决于您是否也可以控制其他网站。 as in you are able to access the code. 因为您可以访问代码。

If you are you can use JSONP to pass the values and get a response, but to do it you will have to assign a callback that is sent and then formatted at the front of a JSON object for it to work (they do this for security). 如果您是您,则可以使用JSONP传递值并获取响应,但是要执行此操作,您将必须分配一个已发送的回调,然后在JSON对象的前端进行格式化才能使其正常工作(为了安全起见,请这样做)。

The other option is to use a php ob_start() function. 另一个选择是使用php ob_start()函数。 (Note: this will only work if the form you are trying to submit these values to allow $_GET to be used to proccess the form) (注意:这仅在您尝试提交这些值以允许$ _GET用于处理表单的表单时有效)

        ob_start();
        include('http://wwww.anotherwebsite.com?key1=value1&key2=value2&key3=value3');
        $returnString = ob_get_contents();
        ob_end_clean();

So then from here $returnString is the result, which you can basically search (strpos() to see if true is how I would do it) in php to find key words to see if it was successful or not or what ever you need to check for. 因此,从这里$ returnString是结果,您基本上可以在php中搜索(strpos()以查看是否为真),找到关键字以查看其是否成功或需要执行的操作检查。

But again, this only works if the form on the remote server uses $_GET and not $_POST (or allows both through globals). 但是同样,这仅在远程服务器上的表单使用$ _GET而不是$ _POST(或通过全局允许两者使用)的情况下才有效。

I know this is a php solution, but a warning is that for security purposes, there are serious restrictions on what javascript can do cross server.. the best javascript way to do cross server is JSONP, which jQuery does support so you might want to look into that.. but as I mentioned, for it to work you need to have a callback be able to be sent back with the response, and the response needs to be in a jsonp object format.. (basically you either need to 1. have the other server have a jsonp api for you to use or you have control over the other server's server side files to make the changes needed). 我知道这是一个php解决方案,但警告是出于安全目的,对于javascript可以跨服务器执行严格限制。.跨服务器的最佳javascript方法是JSONP,jQuery确实支持JSONP,因此您可能希望调查一下..但是正如我提到的那样,要使其正常工作,您需要能够将回调与响应一起发送回去,并且响应必须采用jsonp对象格式。.(基本上,您要么需要1 。让另一台服务器具有供您使用的jsonp api,或者您可以控制另一台服务器的服务器端文件以进行所需的更改)。

Do you want like that? 你想要那样吗? It's simple form submitting to another website. 这是提交到另一个网站的简单表格。 But, I can't check whether it's successfully submitted or not. 但是,我无法检查它是否已成功提交。

<form action="http://www.another.com">
<input name="myInput" type="text">
<input type="submit" value="Submit">
</form>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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