2

我花了最后一个小时左右在这里和谷歌搜索,但没有成功。我有一个表格,我想通过 POST 提交。javascript 运行并验证良好,它甚至“提交”表单。但是当它这样做时,它只是重新加载页面。不发送 POST 标头(在 Firefox 中使用 Firebug 确认)。就好像它只是重新加载页面而不是提交它。

此表单嵌入在 addUser.php 页面中。

<form action="addUser.php" method="post" name="NewUser">
<div data-role="field-contain">
    <label label-for="EmpID" class="ui-hidden-accessible">Enter Employee ID</label>
    <input name="EmpID" id="EmpID" placeholder="Employee ID" value="" autocomplete="off" title="Enter Employee ID" type="number" required autofocus>

    <label label-for="UserName" class="ui-hidden-accessible">Enter username</label>
    <input name="UserName" id="UserName" placeholder="Username" value="" autocomplete="off" title="Enter the new username" required>

    <p>
        <label label-for="NewPass1" class="ui-hidden-accessible">Enter Your Current Password</label>
        <input name="NewPass1" id="NewPass1" placeholder="New Password" value="" type="password" autocomplete="off" title="Must be at least 8 characters, with numbers and upper and lower case letters." required>
    </p>

    <p>
        <label label-for="NewPass2" class="ui-hidden-accessible">Enter Your Current Password</label>
        <input name="NewPass2" id="NewPass2" placeholder="Verify Password" value="" type="password" autocomplete="off" title="Re-enter your chosen new password." required>
    </p>
</div>
<button type="button" name="submitButton" value="submitButton" data-theme="d" data-inline="true" data-icon="check" data-iconpos="left" onclick="validateForm ()">
    Add User
</button>

这是Javascript:

function validateForm()
{
    var reason = "";

    reason += validateEmpID();
    reason += validateUserName();
    reason += validateNewPassword();

    if (reason != "") {
        alert("There are some errors with the data you entered:\n" + reason);
        return false;
    } else {
        document.NewUser.submit();
    }
}

我尝试了各种不同的调用提交方法,结果都一样。非常感谢任何帮助。

谢谢加雷斯

4

4 回答 4

1

我认为应该是

document.forms['NewUser'].submit();

或者

document.forms.NewUser.submit();
于 2013-03-20T04:59:32.130 回答
1

我认为您缺少添加 return false 的地方,这就是您的浏览器要求重新加载表单的原因。更好的提交方法是使用提交按钮而不是使用简单的按钮。

于 2013-03-20T06:24:29.710 回答
0

为什么不使用 AJAX 帖子?或者更好的是 jQuery AJAX POST?

$.post(url, data, datatype);

http://api.jquery.com/jQuery.post/

于 2013-03-20T04:58:01.000 回答
0
function validateForm(event) {
    var reason = "";

    reason += validateEmpID();
    reason += validateUserName();
    reason += validateNewPassword();

    if (reason != "") {
        alert("There are some errors with the data you entered:\n" + reason);
        event.preventDefault();
    }
}

document.getElementById('my_form').addEventListener('submit', validateForm, false);
于 2013-03-20T05:15:11.927 回答