表单中没有发送按钮数据

Form in not sending the button data

本文关键字:按钮 数据 表单      更新时间:2023-09-26

我的表单有一个奇怪的行为,它从文本框发送数据,但不发送按钮数据。

这是我的表格:

<form name="login_registro" method="POST" id="login_registro">
    <input type="text" id="username" name="username" value="">
    <input type="text" id="password" name="password">
    <input type="submit" name="hacer_login" style="width:auto; height:auto; padding:5px;" class="button" onclick="submitForm('{{matches|last}}/entrar')" value="entrar">
    <input type="submit" name="registro" style="width:auto; height:auto; padding:5px;" class="button" onclick="submitForm('{{matches|last}}/registro')"  value="regístrate gratis!" >
</form>

提交功能:

<script type="text/javascript">
    function submitForm(action) {
    document.getElementById('login_registro').action = action;
    document.getElementById('login_registro').submit();
}
</script>

在这两页(entrar和registro)中,我做了一个print_R($_POST);它只显示两个输入,用户名和密码。它没有显示按下的按钮。

如果我删除onclick函数,并添加action="page.php",它将发送两个输入和按下的按钮。

我知道我可以做一个变通办法,但我想知道为什么会发生这种情况。谢谢

附言:我评论了所有jquery。

您的问题是,使用onclick并提交表单,将提交两次,因为提交按钮已经发送了表单,并且您添加了form.submit()

使用onsubmit="return submitForm('actionName')"。如果在函数结束时返回true,则表单将被提交。如果您返回false,提交将被取消。

您可以通过不动态设置操作并将其作为表单的字段或参数(通过submitForm函数设置)发送来更优雅地执行此操作,但这意味着要更改服务器端代码。

function submitForm(action) {
    document.getElementById('login_registro').action = action;
}

我一点也不明白你的回答@Vicentiu Bacioiu,但你给了我一个提示。你说表单提交了两次,所以在函数中删除它提交表单的行对我来说很有效。