HTML表单提交数据
我在一个远程服务器上有一个表单,我正试图向其提交数据,如下例HTML表单提交数据,html,Html,我在一个远程服务器上有一个表单,我正试图向其提交数据,如下例 <html> <head> </head> <form action="http://www.example.com/post.php" method="post"> <input type="text" name="input1" value="test1" /> <input type="text" name="input2" value="test2" /
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
</body>
</html>
我正在尝试像这样自动提交数据
<html>
<head>
</head>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<script type="text/javascript">
document.forms[0].submit();
</script>
</form>
</body>
</html>
document.forms[0]。提交();
如果第一个示例没有(name=“send”)名称,但如果它有名称,则可以正常工作。我的问题是,如何使用带有名称的输入按钮发送数据
谢谢注意:答案原来是
type=“hidden”
而不是type=“submit”
,其中第二个选项不允许提交DOM,同时在GET/POST数据中提交输入值type=“hidden”
可以,它在这里工作,因为不需要实际单击按钮
选择一个:
<form name="formname" id="formid" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
<script type="text/javascript">
if (confirm('Ok for "formname", Cancel for "getElementById"')) {
console.log('document.formname.submit()');
document.formname.submit();
} else {
console.log('document.getElementById("formid").submit()');
document.getElementById('formid').submit();
}
</script>
如果(确认('Ok for“formname”,Cancel for“getElementById”)){
log('document.formname.submit()');
document.formname.submit();
}否则{
console.log('document.getElementById(“formid”).submit()');
document.getElementById('formid').submit();
}
注意:答案是type=“hidden”
而不是type=“submit”
,其中第二个选项不允许DOM提交,同时在GET/POST数据中提交该输入值type=“hidden”
可以,它在这里工作,因为不需要实际单击按钮
选择一个:
<form name="formname" id="formid" action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="submit" name="send" value="Submit" />
</form>
<script type="text/javascript">
if (confirm('Ok for "formname", Cancel for "getElementById"')) {
console.log('document.formname.submit()');
document.formname.submit();
} else {
console.log('document.getElementById("formid").submit()');
document.getElementById('formid').submit();
}
</script>
如果(确认('Ok for“formname”,Cancel for“getElementById”)){
log('document.formname.submit()');
document.formname.submit();
}否则{
console.log('document.getElementById(“formid”).submit()');
document.getElementById('formid').submit();
}
页面加载后,JavaScript代码将立即执行。这就是为什么它会立即提交
您需要将JS代码包装在函数中,并让事件从页面调用它
您可以使用常规按钮并为按钮的onclick事件设置函数
<html>
<head>
<body>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="button" name="send" value="test2" onclick="doSubmit()" />
<script type="text/javascript">
function doSubmit() {
document.forms[0].submit();
}
</script>
</form>
</body>
</html>
函数doSubmit(){
document.forms[0]。提交();
}
您的JavaScript代码将在页面加载后立即执行。这就是为什么它会立即提交
您需要将JS代码包装在函数中,并让事件从页面调用它
您可以使用常规按钮并为按钮的onclick事件设置函数
<html>
<head>
<body>
<form action="http://www.example.com/post.php" method="post">
<input type="text" name="input1" value="test1" />
<input type="text" name="input2" value="test2" />
<input type="button" name="send" value="test2" onclick="doSubmit()" />
<script type="text/javascript">
function doSubmit() {
document.forms[0].submit();
}
</script>
</form>
</body>
</html>
函数doSubmit(){
document.forms[0]。提交();
}
我试过了,我觉得有点混乱不是表单名称它的按钮submit有一个名称,它正在寻找name=“send”没有它的名称它不会提交。它仍然与按钮一起工作:注意,你需要Firefox中的Firebug(或带有控制台、Chrome(本机控制台)或IE9(本机控制台)。是的,您的示例将提交表单,我的第二个示例也将提交表单。问题是远程服务器正在查找name=“send”,如果看不到,则不会提交任何内容。问题是远程服务器正在查找name=“send”在submit按钮上。将其从提交
更改为隐藏
类型。我尝试了它,我认为它有一些混淆,不是表单名称,它的按钮submit有一个名称,它正在寻找name=“send”,没有它就不会提交名称。它仍然与按钮
一起工作:注意,Firefox中需要Firebug(或带有控制台
、Chrome(本机控制台
)或IE9(本机控制台)的内容。是的,您的示例将提交表单,我的第二个示例也将提交表单。问题是远程服务器正在查找name=“send”,如果未看到,则不会提交任何内容。问题是远程服务器正在查找name=“send”在提交按钮上。将其从提交
更改为隐藏
类型。