输入类型提交与输入类型按钮

编程入门 行业动态 更新时间:2024-10-27 07:22:34
本文介绍了输入类型提交与输入类型按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我一直在尝试使用PHP解决烦人的行为(我认为..).也许你们中的一些人遇到了同样的想法.我有一个html表单,我在onClick事件中使用了一个调用javascript函数的元素.处理完脚本的内容后,我将执行form.submit()指令.它基本上检查诸如字段长度等常见问题.

I have been trying to solve an annoying behavior with PHP (I think..). Maybe some of you have encountered the same and have some ideas. I have an html form and I use an element with the onClick event that calls a javascript function. Once the script's content has been processed, I execute the form.submit() directive. It basically checks common things like length of fields, etc.

现在我的问题是,当我使用输入类型按钮时,我将其名称设置为submit1,而为了运行一个小测试,我将设置< form id ="register" action =< ;?php echo($ PHP_SELF)?>"method ="post">

Now my problem is, when I use input type button, I set its name to submit1, and just to run a small test, I set the <form id="register" action="<?php echo($PHP_SELF) ?>" method="post">

然后我输入:

<?php if (isset($_POST["submit1"])) { echo "Submit button was pressed"; die(); } ?>

但是,即使我的输入按钮的名称为Submit1,我也从未看到该消息.然后,我将输入类型更改为提交",然后单击提交",我将看到消息,但是唯一的问题是,即使我的javascript函数在表单中发现任何错误,在警报框上单击确定"后,该表单也会继续进行,无论.它仅在submit type = submit上执行此操作.

However, I never see the message even though my input button's name is submit1. I then changed the input type to submit, and I click submit, I'll see the message but the only problem is that even if my javascript function finds any errors in the form, after clicking okay on the alert box, the form continues regardless. It only does this on submit type=submit.

如果有人感兴趣,

我的按钮:第一次尝试:

my buttons: 1st try:

<input type="button" name="submit1" class="submit" value="Register" onclick="validateFields()" />

第二次尝试:

<input type="submit" name="submit1" class="submit" value="Create Account!" onclick="validateFields()" />

推荐答案

要提交表单,输入字段的类型必须为 submit ,而不是 button .

To submit your form, the input field must be of the type submit, not button.

<input type="submit" name="submit1" class="submit" value="Create Account!" onclick="validateFields()" />

您也可以这样做:

<button type="submit" name="submit1" class="submit" onclick="validateFields()"> Create Account! </button>

并且,为避免在出现错误时提交表单,您可以返回false:

And, to avoid your form from being submitted if there are errors, you can return false:

function validateFields() { //do validation if(failed) { return false; } }

更多推荐

输入类型提交与输入类型按钮

本文发布于:2023-08-01 18:44:47,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1270748.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:类型   按钮

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!