2012-04-25 197 views
0

我有一个表单,当表单通过浏览器加载时,我想让它自动提交数据。我遇到的问题是我写了一个PHP脚本,它将在测试表单上提交一切正常。我的问题是我试图提交数据以命名按钮“提交”的服务器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" /> 
</form> 
<script type="text/javascript"> 
document.forms[0].action="submit" 
</script> 
</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" /> 
<input type="submit" name="submit" class="submit" value="Send Data" /> 
</form> 
</body> 
</html> 

谢谢

+0

这是一个相当本地化的问题。一般来说,在StackOverflow中,最好将问题解释为可以应用于整个开发者社区,而不仅仅是针对您遇到的特定问题。 – 2012-04-25 15:04:39

回答

1

你要提交表单的人的例子吗?然后,只需使用其submit()方法:

document.forms[0].submit(); 

如果服务器预计submit张贴,也就是被点击的按钮,你可以简单地触发click()事件的按钮。既然你不能使用其名称访问它,我会使用jQuery它:

$('input[name="submit"]').click(); 
0

相反的:

<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" /> 
</form> 
<script type="text/javascript"> 
document.forms[0].action="submit" 
</script> 
</body> 
</html> 

试试这个:

<html> 
<head> 
</head> 
<form name="MyForm" action="http://www.example.com/post.php" method="post"> 
<input type="text" name="input1" value="test1" /> 
<input type="text" name="input2" value="test2" /> 
</form> 
<script type="text/javascript"> 
document.MyForm.submit(); 
</script> 
</body> 
</html> 
+0

它会尝试提交数据,但无法识别服务器端的任何内容,例如没有提交任何内容。 – 2012-04-25 08:30:57

0

如果我明白你的问题正确地,

input name =“submit” 和type =“submit”是不同的东西。

因此您也可以轻松创建相同的行为。