javascript 提交()不工作
这是index.php中的表单——我试图通过单击嵌套在“a”锚点内的图像来以编程方式提交它:
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript:document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
这是我的php代码:
<?php
if(isset( $_POST['imageFormSubmitter']))
{
// this fx just echos a javascript 'alert()'
showAlertBox("The index page form 'landingForm' was just submitted.");
}
else
{
var_dump($_GET);
var_dump($_POST);
}
?>
当我在编辑框中键入内容时表单,然后单击图像“bronzeAgeCaveDrawing.jpg”,GET 和 POST 数组的转储显示:
array
Empty
array
Empty
就这一点而言,我什至不确定上面的 PHP 代码是否会被调用,
<a href="javascript:document.landingForm.submit()">
因为上面的情况无论表单是否提交,php 代码都会执行——GET 和 POST 由于上面我添加的“else”子句,当加载index.php时会发生转储,因为“if”子句似乎没有发生。
如果上面的 php 代码实际上是通过 Submit() 调用执行的,则 GET 和 POST 仍然是空数组。
我在一些关于 SO 的帖子中研究了使用带有图像的锚点 'a' 以及调用 Submit() 的示例,但我没有看到 POST 或 GET 数组为空的任何原因。由于我的表单 method="post" 我 100% 期望使用 Submit() 来 POST 表单,而不是使用 GET 提交。
盯着这个有一段时间了——我错过了什么吗?
更新:这是功能代码——感谢您的帮助!
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="itemName_name"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript: document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
if(isset( $_POST['itemName_name']))
{
showAlertBox("The index page form 'landingForm' was just submitted.");
var_dump($_POST);
}
POST 数组转储的输出 NOW 显示:
array
'itemName_name' => string 'fffffffffff' (length=11)
TAKEAWAYS:
1) the syntax "document.landingForm.submit()" is 100% fine.
2) the 'name' attribute of my <img> tag does NOT get posted (not sure why,
but it's not the worst thing happening for me right now so I'll deal with it)
3) giving the text <input> field a 'name' attribute made its way fine to the POST array.
4) the call to submit() does in fact pay attention to the method="post" in my
form declaration.
我正在使用它,谢谢大家。对我来说,有不止一个同样可以接受的答案,所以我会+1你们大家 然后必须选择一个。再次感谢。
Here is the form that is inside index.php -- I'm trying to programmatically submit it using a click on an image that's nested inside an 'a' anchor:
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript:document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
Here is my php code:
<?php
if(isset( $_POST['imageFormSubmitter']))
{
// this fx just echos a javascript 'alert()'
showAlertBox("The index page form 'landingForm' was just submitted.");
}
else
{
var_dump($_GET);
var_dump($_POST);
}
?>
When I type something in the edit box on the form, then click on the image 'bronzeAgeCaveDrawing.jpg' the dump of the GET and POST arrays says:
array
Empty
array
Empty
And for that matter I'm not even sure the above PHP code is getting called for the
<a href="javascript:document.landingForm.submit()">
case at all because the above php code executes regardless of whether the form submits -- the GET and POST
dumps occur when index.php loads due to the 'else' clause above that I added because it didn't appear that the 'if' clause was happening.
If the above php code IS in fact executing by the submit() call, the GET and POST are still empty arrays.
I studied examples of using an anchor 'a' with an image combined with a call to submit() in a few posts on SO and I don't see any reason why the POST or GET array is empty. And due to my form method="post" I'm 100% expecting for the submit() to POST the form, not submit with a GET.
Been staring at this awhile -- am I missing something?
UPDATE: Here is the functioning code -- thanks for your help folks!
<form name="landingForm" id="landingFormId" method="post" action="index.php">
<input type="text" size="250" maxlength="250" name="itemName_name"
id="artifactName">Enter an artifact name here...
</input>
<a href="javascript: document.landingForm.submit()">
<img src="http://localhost/myProj/bronzeAgeCaveDrawing.jpg"
alt="Submit this form" name="imageFormSubmitter" />
</a>
</form>
if(isset( $_POST['itemName_name']))
{
showAlertBox("The index page form 'landingForm' was just submitted.");
var_dump($_POST);
}
Output of the dump of POST array NOW shows:
array
'itemName_name' => string 'fffffffffff' (length=11)
TAKEAWAYS:
1) the syntax "document.landingForm.submit()" is 100% fine.
2) the 'name' attribute of my <img> tag does NOT get posted (not sure why,
but it's not the worst thing happening for me right now so I'll deal with it)
3) giving the text <input> field a 'name' attribute made its way fine to the POST array.
4) the call to submit() does in fact pay attention to the method="post" in my
form declaration.
I'm running with it, thanks all. There is more than one equally acceptable answer for me, so I'll +1 y'all
then have to choose one. Thanks again.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您没有在
标记上设置
name
属性,并且![]()
标记不是正确的方法提交图像。来源:HTML Dog 标签参考
另请注意
标签没有/不能有子级。
You're not setting the
name
attribute on your<input>
tag, and the<img>
tag is not the right way to submit with an image.Source: HTML Dog tag reference
Note also that the
<input>
tag in HTML5 does not / cannot have children.