简体   繁体   English

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

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

Is there a way to make a form submit "onload" without clicking a submit button with PHP? 有没有一种方法可以使表单提交“ onload”而不用PHP单击提交按钮? i saw some ways to make it with Javascript, but i love php and id like to make it with php. 我看到了一些用Javascript制作的方法,但我喜欢php和id,喜欢用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? 好吧,我们可以用PHP做同样的事情吗? Could you please kindly share your ideas and advices? 您能否分享您的想法和建议?

Thank you. 谢谢。

It isn't possible to carry out a client-side (ie: browser based) form submit action using a server side language (ie: PHP). 无法使用服务器端语言(即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.) (您可以从图书馆获取书籍,然后将其退还到那里,但是图书馆本身无法阅读书籍。)

As such, JavaScript is your only solution if you require an automated submission. 因此,JavaScript是您唯一需要自动提交的解决方案。 Even then, you need to ensure there's a "normal" solution for use by people who don't have JavaScript enabled. 即使那样,您仍需要确保有一个“常规”解决方案供未启用JavaScript的人使用。 (The joy of graceful degradation, etc.) (优雅降级的喜悦等)

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. 该curl命令将文件发布到url并发回它收到的所有数据。 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. 它正在发布一个xml文件,这就是为什么将内容类型设置为application / x-www-form-urlencoded的原因。我正在发布的api要求对数据进行编码,这就是我使用--data-urlencode的原因。 I also assign the filename with a post variable name here so its like posting an input field with a name="whatever". 我还在这里给文件名分配了一个后置变量名,因此它就像发布一个带有名称=“ 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);

Ps DO NOT FORGET TO ESCAPE ANY '=' or '&' in your url's, this causes issues if you're trying to retrieve data. Ps不要忘记在网址中转义任何'='或'&',如果您尝试检索数据,这会导致问题。

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? 如果您想在页面加载后自动提交表单(恕我直言,这毫无意义),我假设您是自己(通过PHP代码)生成了它的值,并且想将它们发送到另一个页面,对吗? If so redirect page to the another by header() passing values through POST: 如果是这样,则通过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();

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. 我为地图服务做了很多。

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重播我,我会给您帮助

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

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