繁体   English   中英

每个文件中的代码都有效,但是记录每个表中的记录重复两次

[英]code in each file is working but records entry repeats for two times in each table

验证过程是正确的,数据输入也正在发生,但是在表单提交时,记录在每个表中插入了两次。

**library.php HTML CODE**

        <form id="book_entry_form">
          <table class="table borderless table-condensed">                  
            <tr>
              <td><label for="entry_isbn">ISBN No.:</label></td>
              <td><input type="text" id="entry_isbn" class="form-control input-sm eqtxt" name="entry_isbn"></td>
            </tr>
            <tr>
              <td><label for="entry_subject">Subject:</label></td>
              <td><input type="text" id="entry_subject" class="form-control input-sm eqtxt" name="entry_subject"></td>
            </tr>

            <tr>
              <td></td>
              <td>
                <button id="entry_sub_btn" type="submit" class="btn btn-primary logbtn">Submit</button>
                &nbsp;&nbsp;&nbsp;&nbsp;
                <button href="#" type="reset" class="btn btn-primary logbtn">Refresh</button>
              </td>
            </tr> 
          </table><!-- table closed -->    
        </form><!-- form -->

lib_book_reg.js Javascript代码

$('#book_entry_form').bootstrapValidator({
                group: 'td',
                feedbackIcons: {
                    valid: 'glyphicon glyphicon-ok',
                    invalid: 'glyphicon glyphicon-remove',
                    validating: 'glyphicon glyphicon-refresh'
                },
                fields: {
                    entry_isbn: {
                        message: 'The ISBN NO. is not valid',
                        validators: {
                            notEmpty: {
                                message: 'ISBN No. is required and cannot be empty'
                            }
                        }
                    },            

                    entry_subject: {
                       message: 'The subject name is not valid',
                        validators: {
                            notEmpty: {
                                message: 'Subject name is required and cannot be empty'
                            }
                        }
                    }             
                }
            })


        .on('success.field.bv', function(e, data) {
                    if (data.bv.isValid()) {
                        data.bv.disableSubmitButtons(false);
                        $('#entry_sub_btn').on('click',function(e){

            e.preventDefault();
            e.stopPropagation();


           var entry_isbn = $('#entry_isbn').val();
           var entry_subject= $('#entry_subject').val();

           $.ajax({
              url: 'php/lib_book_reg.php',
              type: 'post',
              data: { 'entry_isbn':entry_isbn, 'entry_subject':entry_subject },
              success: function(data) {
                alert(data);
                window.location.reload(true);

              },
              cache: false
            }); // end ajax call
         }); // end of button click event
         } //end of if statement
         }); // end of main function

lib_book_reg.php PHP代码

<?php
    session_start();
    if($_SESSION['user'] != "Library")
    {
        echo "Only Librarian is authorized to make an entry!";
    exit();
    }
    require 'config.php';

    // $eid = $_POST['eid'];
    $entry_isbn = $_POST['entry_isbn'];
    $entry_subject = $_POST['entry_subject'];


    $stmt = $dbh->prepare("INSERT INTO lib_book_reg(l_book_isbn, l_book_subject)    VALUES(?,?)");
    $stmt->execute(array($entry_isbn, $entry_subject));

    $stmt1 = $dbh->prepare("INSERT INTO l_book_opr_db(l_book_isbn, l_book_subject)  VALUES(?,?)");
    $stmt1->execute(array($entry_isbn, $entry_subject));

    echo "Registered Successfully";
    ?>

尝试在点击事件后禁用按钮。 尝试添加(您可以在超时后或ajax成功后启用它)

$(this).attr('disabled',true);

$('#entry_sub_btn').on('click',function(e){

更新

这也可能是因为为该按钮分配了两次click事件。

因此在分配之前,只需取消绑定旧的click事件并分配新的事件即可。

样例代码

$('#entry_sub_btn').off('click').on('click',function(e){

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM