输入类型提交与输入类型按钮 [英] input type submit vs input type button

查看:42
本文介绍了输入类型提交与输入类型按钮的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在尝试使用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;
    }
}

这篇关于输入类型提交与输入类型按钮的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆