FormData不包含按钮Javascript [英] FormData does't include the button Javascript
问题描述
我遇到 FormData
的问题,它几天前工作但现在它不起作用,它提交除提交按钮之外的所有输入。这是我的登录表单。
I'm having a problem with FormData
, it was working a couple days ago but now it doesn't work, it submits all the inputs except the submit button. Here's my login form.
<form action="" method="post" name="login_user">
<label for="username">Username/e-mail <span class="error" id="user-error"></span></label>
<input type="text" name="username" id="username" required>
<br>
<label for="passwd">Password <span class="error" id="passwd-error"></span></label>
<input type="password" name="passwd" id="passwd" required>
<br>
<p><input type="checkbox" checked name="remember"> Remember me <span class="forgot"><a href="<?php echo SITE_URL; ?>/reset">Forgotten password</a></span> </p>
<input type="submit" id="login" value="Login" name="login">
<p>Don't have an account? <a href="<?php echo SITE_URL; ?>/register/">Sign up</a></p>
</form>
用于登录的JS,使用MVC。
JS for login, uses MVC.
//ajax login
var login = document.forms.namedItem('login_user');
if (login) {
login.addEventListener('submit', function (e) {
if (validatePassword('passwd', 'passwd-error')) {
var data = new FormData(login);
var userlogin = new XMLHttpRequest();
var btn = document.getElementById('login');
btn.value = 'Login, Please wait...';
userlogin.open('POST', url + 'login/login_user_ajax', true);
userlogin.onload = function (event) {
if (userlogin.status == 200) {
var result = JSON.parse(userlogin.responseText);
if (result.results == 'success.') {
alert('logged in'); //change this later
} else {
document.getElementById('cred-error').innerHTML = result.results;
}
btn.value = 'Login';
}
};
userlogin.send(data);
}
e.preventDefault();
}, false);
我的控制器中的登录方法,未检测到按钮。
The login method in my controller, the button is not detected.
public function login_user_ajax() {
$this->login_user(1);
}
private function login_user($ajax = '')
{
if (filter_has_var(INPUT_POST, 'login')) {
$new_user = $this->model('User');
$new_user->setDb(Controller::getDb());
$new_user->set_site(SITE_URL);
$user = trim(filter_input(INPUT_POST, 'username', FILTER_SANITIZE_STRING));
$passwd = trim(filter_input(INPUT_POST, 'passwd', FILTER_SANITIZE_STRING));
if ($new_user->login($user, $passwd)) {
if ($ajax) {
$site_data['results'] = 'success.';
echo json_encode($site_data);
return true;
}else {
$this->redirect(SITE_URL . '/edit');
}
} else {
if (!$ajax){
return 'The username or password is incorrect.';
}else {
$site_data['results'] = 'The username or password is incorrect.';
echo json_encode($site_data);
return false;
}
}
}else {
print_r($_POST);
}
}
我在时得到这个print_r
$ _ POST
,没有按钮。
推荐答案
因为你实际上并没有使用默认提交(而是你正在做ajax),需要自己添加点击按钮。一种简单的方法是使用您希望按钮具有的名称向表单添加隐藏输入,然后让表单中的所有按钮使用此单击处理程序:
Because you're not actually using the default submit (instead you're doing ajax), you need to add the clicked button yourself. One easy way to do this is to add a hidden input to your form with the name you want the button to have, and then have all the buttons in the form use this click handler:
function clickHandler() {
this.form.theHiddenInput.value = this.value;
}
这样,如果按钮被用于提交表单,按钮的处理程序在 submit
之前设置隐藏输入的值。
That way, if a button was used to submit the form, the button's handler sets the value of the hidden input prior to the submit
.
这篇关于FormData不包含按钮Javascript的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!