簡體   English   中英

preventDefault()無法與form.submit()一起使用

[英]preventDefault() won't work with form.submit()

我正在嘗試獲取登錄表單以關閉燈箱,或根據登錄嘗試是否成功來更改錯誤框的文本。 我不確定,但我認為這與我的

 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函數移到Submit函數中

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM