javascript 自动提交表单
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32879625/
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
Automatically submit a form
提问by user3513237
I am trying to get a form to submit automatically, which feeds a username and password to another form.
我试图让一个表单自动提交,它将用户名和密码提供给另一个表单。
If I remove the javascript and just have the form display on the screen, and manually click the Submit button, the result is I get automatically logged in on the resulting page. This is the result I want.
如果我删除 javascript 并只在屏幕上显示表单,然后手动单击提交按钮,结果是我会自动登录到结果页面。这就是我想要的结果。
If I leave the javascript in place, it actually doesn't automatically log me in but it does pass across the username and password pre-filled on the resulting page, then I have to click Submit on the resulting page.
如果我将 javascript 留在原地,它实际上不会自动让我登录,但它会传递在结果页面上预先填写的用户名和密码,然后我必须在结果页面上单击提交。
How can I make it automatically submit the form to work the same way as a user actually hitting the submit button? Also I understand that this may not be the most secure way to pass a username and password across, but this is for a special use case.
如何让它自动提交表单以与用户实际点击提交按钮的方式相同?我也知道这可能不是传递用户名和密码的最安全方式,但这是针对特殊用例的。
Note: MyForm is a php page, and I am submitting to an aspx page.
注意:MyForm 是一个 php 页面,我提交到一个 aspx 页面。
Here is code of what my form would look like:
这是我的表单的代码:
<form id='myForm' method='post' action='https://blah.com/Login.aspx'>";
<p>Username: <input value='usernameHere' name='Username' type='text' id='Username' data-index='0' maxlength='255' placeholder='Username' class='loginUsername' />";
<p>Password: <input value='passwordHere' name='Password' type='password' id='Password' placeholder='Password' />";
<p><input type='submit' name='btnSignIn' value='Sign In' id='btnSignIn' />
</form>
<script type="text/javascript">
document.getElementById('myForm').submit();
</script>
Thank you.
谢谢你。
回答by
I suspect there are other issues at play, as using JS to submit the form should replicate a native browser submit.
You can try simulating clicking the 'submit' button via JavaScript:
我怀疑还有其他问题在起作用,因为使用 JS 提交表单应该复制本机浏览器提交。
您可以尝试通过 JavaScript 模拟单击“提交”按钮:
JavaScript:
JavaScript:
document.getElementById('btnSignIn').click();
jQuery:
jQuery:
$('#btnSignIn').click();
回答by Muhammad Usman
You can use onload
method for javascript
您可以使用javascript的onload
方法
function submitForm() {
// **NOTE** set form values first
document.getElementById('myForm').submit();
}
window.onload = submitForm;
Or with jQuery if you want:
或者如果需要,可以使用 jQuery:
$(function(){
submitForm();
});
or with tag attribute
或带有标签属性
he typical options is using the onload event:
典型的选项是使用 onload 事件:
<body onload="javascript:submitForm()">
.
.
回答by Rohit Kumar
As the problem is supposed "post a form through php" . It may be achived by using PHP?s functions fsockopen() and fputs() to send properly formatted data to a remote server. Here is a sample of code :
由于问题应该是“通过 php 发布表单”。它可以通过使用 PHP 的函数 fsockopen() 和 fputs() 将格式正确的数据发送到远程服务器来实现。这是代码示例:
<?php
//create array of data to be posted
$post_data['Username'] = 'XYZ';
$post_data['Password'] = '********';
$post_data['btnSignIn']="Sign In";
//traverse array and prepare data for posting (key1=value1)
foreach ( $post_data as $key => $value) {
$post_items[] = $key . '=' . $value;
}
//create the final string to be posted using implode()
$post_string = implode ('&', $post_items);
//we also need to add a question mark at the beginning of the string
$post_string = '?' . $post_string;
//we are going to need the length of the data string
$data_length = strlen($post_string);
//let's open the connection
$connection = fsockopen('https://blah.com', 80);
//sending the data
fputs($connection, "POST /Login.aspx HTTP/1.1\r\n");
fputs($connection, "Host: https://blah.com \r\n");
fputs($connection,
"Content-Type: application/x-www-form-urlencoded\r\n");
fputs($connection, "Content-Length: $data_length\r\n");
fputs($connection, "Connection: close\r\n\r\n");
fputs($connection, $post_string);
//closing the connection
fclose($connection);
?>
The above codes do post your form now to get redirect to that page , use simply
上面的代码现在发布您的表单以重定向到该页面,只需使用
header('location :https://blah.com/Login.aspx ');