preventDefault()不适用于form.submit()

时间:2014-03-10 01:26:15

标签: javascript php jquery html ajax

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

有关
 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}”。有什么建议? (请记住,它需要尽可能安全)

1 个答案:

答案 0 :(得分:0)

您可以尝试将其全部移动到单个处理程序调用中。通过将表单上的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