Javascript preventDefault()不适用于表单。submit()
我正在尝试让我的登录表单关闭我的lightbox,或者根据登录尝试是否成功更改errorbox的文本。我不确定,但我认为这与我的工作有关Javascript preventDefault()不适用于表单。submit(),javascript,php,jquery,html,ajax,Javascript,Php,Jquery,Html,Ajax,我正在尝试让我的登录表单关闭我的lightbox,或者根据登录尝试是否成功更改errorbox的文本。我不确定,但我认为这与我的工作有关 onclick="formhash(this.form, this.form.password);" 这是一个JS函数,它散列一个密码,然后继续表单提交。它以 form.submit(); 这是我的密码: HTML: JS: PHP: 当我登录时,默认设置不起作用。它在新页面中打开process_login.php,并在空白页面上显示{succ
onclick="formhash(this.form, this.form.password);"
这是一个JS函数,它散列一个密码,然后继续表单提交。它以
form.submit();
这是我的密码:
HTML:
JS:
PHP:
当我登录时,默认设置不起作用。它在新页面中打开process_login.php,并在空白页面上显示{success:true}。有什么建议吗?请记住,它需要尽可能安全您可以尝试将其全部移动到单个处理程序调用中。将表单上的type=按钮更改为type=submit
<form action="includes/process_login.php" method="post" name="login_form">
Email: <input class="searchform" type="text" name="email" size="20"/><br />
Password: <input class="searchform" type="password" name="password" id="password" size="20"/><br />
<input type="submit" class="searchform" value="Submit" size="40" style="height:45px; width:90px" />
<input type="text" id="errorbox" style="height:45px; width:180px" value=""><br>
您确定已触发提交事件吗?使用警报或console.log进行调试;我也会使用一个ID,而不是表单名。formhash函数在哪里?如果以submit操作结束,则无论上述事件的preventDefault如何,表单都将被提交。我认为event.preventDefault与submit事件无关,请尝试使用clck@Sebweb-preventDefault在处理程序被调用的情况下与submit一起工作。但是form.submit可能绕过了处理程序。那么有什么办法可以使用formhash和preventDefault呢?
<script>
!(function($){
$(function() {
$('form[name="login_form"]').on('submit', function(event){
event.preventDefault();//don't reload the page
$.post('includes/process_login.php', $(this).serialize(), function(data){
//data is a json object which contans the reponse
data = $.parseJSON(data);
$("fade").fadeOut();
$("light").fadeOut();
},
function(data){//error callback
data = $.parseJSON(data);
if(data.forbidden){
$("#errorBox").html("Error!");
}
else if(data.error){
$("#errorBox").html("Invalid request!");
}
});
});
return false;
});
})(window.jQuery);
</script>
<?php
include_once 'db_connect.php';
include_once 'functions.php';
sec_session_start(); // Our custom secure way of starting a PHP session.
$response = array();
if (isset($_POST['email'], $_POST['p'])) {
$email = $_POST['email'];
$password = $_POST['p'];
if (login($email, $password, $mysqli) == true) {
http_response_code(200);//HTTP OK, requires php 5.4
$response['success'] = true;
} else {
// Login failed
$response['error'] = true;
http_response_code(401);//HTTP forbidden
}
} else {
// The correct POST variables were not sent to this page.
$response['error'] = true;
http_response_code(400);//HTTP bad request
}
echo json_encode($response);
<form action="includes/process_login.php" method="post" name="login_form">
Email: <input class="searchform" type="text" name="email" size="20"/><br />
Password: <input class="searchform" type="password" name="password" id="password" size="20"/><br />
<input type="submit" class="searchform" value="Submit" size="40" style="height:45px; width:90px" />
<input type="text" id="errorbox" style="height:45px; width:180px" value=""><br>
!(function($){
$(function() {
$('form[name="login_form"]').on('submit', function(event){
event.preventDefault();//don't reload the page
formhash(var1, var2);
$.post('includes/process_login.php', $(this).serialize(), function(data){
//data is a json object which contans the reponse
data = $.parseJSON(data);
$("fade").fadeOut();
$("light").fadeOut();
},
...//the rest of your code