gpt4 book ai didi

javascript - PreventDefault() 不适用于 form.submit()

转载 作者:行者123 更新时间:2023-11-28 08:26:58 26 4
gpt4 key购买 nike

我正在尝试让我的登录表单关闭我的灯箱,或更改错误框的文本,具体取决于登录尝试是否成功。我不确定,但我认为这与我的

 onclick="formhash(this.form, this.form.password);"

这是一个 JS 函数,它对密码进行哈希处理,然后继续表单提交。它结束于

    form.submit();

这是我的代码:

HTML:

<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="button" class="searchform"
value="Submit" size="40" style="height:45px; width:90px"
onclick="formhash(this.form, this.form.password);" />
<input type="text" id="errorbox" style="height:45px; width:180px" value=""><br>
</form>

JS:

<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:

<?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);



当我登录时,preventDefault() 不起作用。它在新页面中打开 process_login.php 并在空白页面上显示“{"success":true}"。有什么建议么? (请记住,它需要尽可能安全)

最佳答案

您可以尝试将其全部移至单个处理程序调用中。将表单上的 type="button" 更改为 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>

然后您可以将 formhash 函数移至提交函数

!(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

关于javascript - PreventDefault() 不适用于 form.submit(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22290627/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com