php 如何在不单击提交按钮的情况下在页面加载时提交表单?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4734901/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:11:42  来源:igfitidea点击:

How to submit a form on page load without clicking submit button?

php

提问by Andrew

Is there a way to make a form submit "onload" without clicking a submit button with PHP? i saw some ways to make it with Javascript, but i love php and id like to make it with php.

有没有办法让表单提交“onload”而不用PHP点击提交按钮?我看到了一些用 Javascript 制作它的方法,但我喜欢 php 并且我喜欢用 php 制作它。

For example:

例如:

<form action="https://xxxxxx.com/yyy.php" method="post" id="foo">
<input type="hidden" name="LoginEmail" value="user [at] domain.com">
<input type="hidden" name="LoginPassword" value="mycleartextpassword">
<input type="submit" name="Authenticate" value="Login">
<input type="hidden" name="Action" value="1" >
</form>

I can make it with javascript:

我可以用 javascript 来实现:

<form id="foo"> .... < /form>

<script type="text/javascript">
    function myfunc () {
        var frm = document.getElementById("foo");
        frm.submit();
    }
    window.onload = myfunc;
</script>

Well, Can we make same thing with PHP? Could you please kindly share your ideas and advices?

那么,我们可以用 PHP 做同样的事情吗?能否请您分享您的想法和建议?

Thank you.

谢谢你。

回答by John Parker

It isn't possible to carry out a client-side (i.e.: browser based) form submit action using a server side language (i.e.: PHP). It's a bit like trying to get a library to read a book, if that makes sense. (You get the books from the library and return them there, but the library itself can't read books.)

使用服务器端语言(即:PHP)执行客户端(即:基于浏览器)表单提交操作是不可能的。如果有意义的话,这有点像试图让图书馆阅读一本书。(你从图书馆拿书然后还给那里,但图书馆本身不能读书。)

As such, JavaScript is your only solution if you require an automated submission. Even then, you need to ensure there's a "normal" solution for use by people who don't have JavaScript enabled. (The joy of graceful degradation, etc.)

因此,如果您需要自动提交,JavaScript 是您唯一的解决方案。即便如此,您也需要确保有一个“正常”的解决方案供没有启用 JavaScript 的人使用。(优雅退化的喜悦等)

回答by rachael

i had diffculty in dong this as well so thought i would post my solution here it goes...

我在东这也有困难,所以我想我会在这里发布我的解决方案......

this curl command posts the file to a url and send back any data it receives. It's posting an xml file which is why the content type is set to application/x-www-form-urlencoded.The api i was posting to requested that the data be encoded which is why I use --data-urlencode. I also assign the filename with a post variable name here so its like posting an input field with a name="whatever".

这个 curl 命令将文件发布到一个 url 并发送回它接收到的任何数据。它发布了一个 xml 文件,这就是为什么将内容类型设置为 application/x-www-form-urlencoded。我发布的 api 要求对数据进行编码,这就是我使用 --data-urlencode 的原因。我还在此处为文件名分配了一个 post 变量名称,因此就像发布一个名称为 =“whatever”的输入字段一样。

curl --data-urlencode [post variable name]=@[filename] --header "Content-Type:application/x-www-form-urlencoded" http://[url to send file to]

Assigning the command to a variable is easy...see below '

将命令分配给变量很容易......见下文'

content=$(put curl command in here);

P.s DO NOT FORGET TO ESCAPE ANY '=' or '&' in your url's, this causes issues if you're trying to retrieve data.

Ps 不要忘记在您的 url 中转义任何 '=' 或 '&',如果您尝试检索数据,这会导致问题。

回答by marines

If you want to auto-submit form after page load (IMHO it's pointless) I assume you generated its values by yourself (by PHP code) and you want to send them to another page, right? If so redirect page to the another by header() passing values through POST:

如果您想在页面加载后自动提交表单(恕我直言,这是毫无意义的)我假设您自己(通过 PHP 代码)生成了它的值,并且您想将它们发送到另一个页面,对吗?如果是这样,则通过 header() 通过 POST 传递值将页面重定向到另一个页面:

$post_data = 'LoginEmail=username [at] domain.com&LoginPassword=...';
$content_length = strlen($post_data);

header('POST /another_page.php HTTP/1.1');
header('Host: fancyhost');
header('Connection: close');
header('Content-type: application/x-www-form-urlencoded');
header('Content-length: ' . $content_length);
header('');
header($post_data);

exit();

回答by sibiraj

it is by using jquery ajax.

它是通过使用 jquery ajax。

$(document).ready(function(){..........your code goes here........});

if you prefer jquery replay me i wll give you the help

如果你更喜欢 jquery 重播我,我会给你帮助

回答by developer68

Use printf to print your javascript code then when the client browser receives the page it will execute your javascript.

使用 printf 打印您的 javascript 代码,然后当客户端浏览器收到页面时,它将执行您的 javascript。

printf("<script type="text/javascript">
    function myfunc () {
        var frm = document.getElementById("foo");
        frm.submit();
    }
    window.onload = myfunc;
</script>");

I do this a lot for the mapping services.

我为地图服务做了很多事情。