如何使href充当提交表单

时间:2022-08-23 23:02:06

So I have a a href setup as a simple form submit, but I wanted to use it as the same type of method as a submit on a form.

所以我有一个href设置作为一个简单的表单提交,但我想将它用作与表单上的提交相同类型的方法。

So the way I have it setup is:

所以我设置它的方式是:

<!-- HTML -->
<a onclick="subForm('login');" style="cursor: pointer;"> login </a>

// JavaScript
function subForm(el) {
  var elem = document.getElementById(el);
  elem.submit();
}

Now when I click on that link it does act as if I have clicked submit on a form, I can set up a input type=hidden for the form so I can see it in PHP but..

现在,当我点击该链接时,它确实表现为我在表单上单击了提交,我可以为表单设置输入类型=隐藏,这样我就可以在PHP中看到它但是..

I needed for this login system to not be empty and didn't want to use the older method with if empty then display an error after processed, so I wanted the submit button to be able to do the same as clicking submit on a form when one of the inputs are set to required in their markup tag..

我需要这个登录系统不是空的,并且不想使用旧的方法,如果为空则在处理后显示错误,所以我希望提交按钮能够像在表单上单击提交一样其中一个输入在其标记标记中设置为必需..

2 个解决方案

#1


1  

Your code should work assuming that that

你的代码应该假设那样

<a onclick="subForm('login');" style="cursor: pointor;"> login </a>

is inside a form that has an id of 'login';

在一个id为'login'的表单中;

A couple of things you might want to know is that cursor:pointor is wrong, it should be cursor:pointer. However, if you change your code slightly then you won't need the inline style anyway. Something like this would be a good improvement in my opinion. I've not tested it but it should work:

你可能想知道的一些事情是游标:pointor是错误的,它应该是cursor:pointer。但是,如果稍微更改代码,则无论如何都不需要内联样式。在我看来,这样的事情会有很大改善。我没有测试它但它应该工作:

<form id="login" action="/wherever/you/want/this/form/to/post/to" method="post">
    <a id="js-submit-form" href=""> login </a>
</form>

<script>
    var formToSubmit = document.getDocumentById('login'),
    button = document.getElementById('js-submit-form'),
    submitForm = function(e) {
        e.preventDefault(); // this will stop the browser trying to use the href value
        formToSubmit.submit();
    };

    button.addEventListener("click",submitForm);

</script>

I've just read the rest of your question but I don't actually understand what you're asking. Why wouldn't you just use the 'required' tag and some server-side validation?

我刚刚读了你的其余问题,但我实际上并不明白你在问什么。为什么不使用'required'标签和一些服务器端验证?

#2


1  

You probably want to make a JavaScript Validation of the form fields.

您可能希望对表单字段进行JavaScript验证。

This may help you as well related to what you want

这可能会帮助您与您想要的相关

#1


1  

Your code should work assuming that that

你的代码应该假设那样

<a onclick="subForm('login');" style="cursor: pointor;"> login </a>

is inside a form that has an id of 'login';

在一个id为'login'的表单中;

A couple of things you might want to know is that cursor:pointor is wrong, it should be cursor:pointer. However, if you change your code slightly then you won't need the inline style anyway. Something like this would be a good improvement in my opinion. I've not tested it but it should work:

你可能想知道的一些事情是游标:pointor是错误的,它应该是cursor:pointer。但是,如果稍微更改代码,则无论如何都不需要内联样式。在我看来,这样的事情会有很大改善。我没有测试它但它应该工作:

<form id="login" action="/wherever/you/want/this/form/to/post/to" method="post">
    <a id="js-submit-form" href=""> login </a>
</form>

<script>
    var formToSubmit = document.getDocumentById('login'),
    button = document.getElementById('js-submit-form'),
    submitForm = function(e) {
        e.preventDefault(); // this will stop the browser trying to use the href value
        formToSubmit.submit();
    };

    button.addEventListener("click",submitForm);

</script>

I've just read the rest of your question but I don't actually understand what you're asking. Why wouldn't you just use the 'required' tag and some server-side validation?

我刚刚读了你的其余问题,但我实际上并不明白你在问什么。为什么不使用'required'标签和一些服务器端验证?

#2


1  

You probably want to make a JavaScript Validation of the form fields.

您可能希望对表单字段进行JavaScript验证。

This may help you as well related to what you want

这可能会帮助您与您想要的相关