将onclick函数添加到提交按钮 [英] add onclick function to a submit button
问题描述
我正在学习javascript和php.我创建了一个联系表单,并且希望按下提交按钮时可以完成两件事:
I'm just learning javascript and php. I created a contact form and I'd like the submit button to accomplish two things when I press it:
- 将数据提交给我(这部分正在工作)
- 阅读我的onclick函数(此部分不起作用)
<input id="submit" name="submit" type="submit" value="Submit" onclick="eatFood()">
<?php
if ($_POST['submit']) {
////?????
}
?>
我正在将数据发送到我的电子邮件中,所以我明白了.但是onclick
函数似乎不起作用.我尝试查看为提交按钮添加onclick函数,但这没有帮助.
I'm sending the data to my email, and so I get that. But the onclick
function doesn't seem to work. I tried reviewing add onclick function for submit button but it didn't help.
推荐答案
我需要查看您的提交"按钮html标签以获得更好的帮助.我不熟悉php及其如何处理回发,但是我猜根据您要执行的操作,您有以下三种选择:
I need to see your submit button html tag for better help. I am not familiar with php and how it handles the postback, but I guess depending on what you want to do, you have three options:
- 获取客户端上的处理
onclick
按钮:在这种情况下,您只需要调用javascript函数即可.
- Getting the handling
onclick
button on the client-side: In this case you only need to call a javascript function.
function foo() {
alert("Submit button clicked!");
return true;
}
<input type="submit" value="submit" onclick="return foo();" />
-
如果要处理服务器端的单击,则应首先确保将表单标记方法属性设置为
post
:
<form method="post">
您可以使用form
本身的onsubmit
事件将函数绑定到该事件.
You can use onsubmit
event from form
itself to bind your function to it.
<form name="frm1" method="post" onsubmit="return greeting()">
<input type="text" name="fname">
<input type="submit" value="Submit">
</form>
这篇关于将onclick函数添加到提交按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!