php 表单中的图像提交按钮
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8076992/
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
Image Submit Button within a Form
提问by user311509
I have a form that has Submit button in form of an Image. When user clicks the image button, the image button should play the role of submit button.
我有一个具有图像形式的提交按钮的表单。当用户点击图片按钮时,图片按钮应该起到提交按钮的作用。
Code Sample:
代码示例:
<form action="page.php" method="POST">
<input type="image" name="btn_opentextbox" src="image.png" value="Submit" />
</form>
Handle Submission:
处理提交:
if($_POST['btn_opentextbox'])
{
//do something
}
Surprisingly, the above code used to work perfectly fine in Firefox. However, once i updated my Firefox yesterday, it didn't work at all. I click the button, page gets refreshed and nothing happens. The code also doesn't work in IE.
令人惊讶的是,上面的代码过去在 Firefox 中运行得非常好。然而,昨天我更新了我的 Firefox 后,它根本不起作用。我点击按钮,页面刷新,没有任何反应。该代码在 IE 中也不起作用。
Note: it works in Chrome.
注意:它适用于 Chrome。
I want it to work in Firefox, IE, etc.
我希望它在 Firefox、IE 等中工作。
Any suggestions?
有什么建议?
采纳答案by beta
you can add a hidden field
你可以添加一个隐藏字段
<input type="hidden" name="action" value="Submit Form">
and in php you can do this
在 php 中你可以这样做
if($_POST['action'] == "Submit Form"){
do something
}
hope this help.
希望这有帮助。
回答by Lukas Knuth
You should use a normal submit
-button and use CSS to replace the button's look with an image. This should work in all browsers.
您应该使用普通的submit
-button 并使用 CSS 将按钮的外观替换为图像。这应该适用于所有浏览器。
回答by Sonal Khunt
for image submit button
用于图像提交按钮
php code is
php代码是
if(isset($_POST['btn_opentextbox_X']) || isset($_POST['btn_opentextbox_Y']))
{
//do something
}
回答by Anthony
The good php code is :
好的 php 代码是:
<input type='image' src='../images/blanc.gif' width='596' height='35' onFocus='form.submit' name='btn_opentextbox'/>
if ($_POST["btn_opentextbox_x"]) && ($_POST["btn_opentextbox_y"])
{
......
}
回答by Quentin
Check for btn_opentextbox_x
or btn_opentextbox_y
instead. (It is actually .
not _
but PHP mangles it).
检查btn_opentextbox_x
或btn_opentextbox_y
代替。(实际上.
不是,_
而是 PHP 破坏了它)。
Some browsers fail to send the valuefor server side image maps, just the co-ordinates.
一些浏览器无法发送服务器端图像映射的值,只是坐标。
And you seem to have forgotten the alt
attribute.
而你似乎忘记了这个alt
属性。
Alternatively, use an actual submit button instead of an image map:
或者,使用实际的提交按钮而不是图像映射:
<button type="submit" name="btn_opentextbox" value="submit"><img src="image.png" alt="Submit"></button>
… but note that some versions of IE will send the HTML content instead of the value when it is submitted.
...但请注意,某些版本的 IE 会在提交时发送 HTML 内容而不是值。
回答by Peter
Do you have multiple buttons in that form and need to know that the form was submitted? If there is just a single submit button I suggest using following code:
您是否在该表单中有多个按钮并且需要知道该表单已提交?如果只有一个提交按钮,我建议使用以下代码:
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
// process form submission
header('Location: page.php?result=success');
}
This way you'll be sure if form was submitted and also avoid double submission if user hits reload button after form was submitted.
通过这种方式,您可以确定表单是否已提交,并且如果用户在表单提交后点击重新加载按钮,也可以避免重复提交。