Ajax表单提交内部循环提交不止一次

时间:2016-10-17 13:06:33

标签: php jquery ajax forms

我在循环中有一个ajax脚本和表单 - (每个用户一个)。 虽然表单和提交按钮都具有用户的唯一ID。但是,只要我按下按钮,它们仍然会被提交多次。

这是我的剧本

<?php $results = $con->query("SELECT * FROM tbl_users"); ?>
          <?php while($result = $results->fetch_object()): ?>
            <div class="chat" data-chat="person<?=$result->user_id?>">
                <div class="conversation-start">
                </div>

                <?php 
                $sender = $result->user_id;
                $messageload = $con->query("SELECT * FROM messages WHERE sender = $sender"); ?>

                <?php while($message = $messageload->fetch_object()): ?>
                  <div class="bubble you">
                      Hello,
                  </div>
                  <div class="bubble me">
                      Are you serious?
                  </div>
                <?php endwhile;?>
            </div>

            <div class="write">
            <form autocomplete="off" id="SendForm<?=$result->user_id?>" class="SendMsg" role="form" method="post">
                <input type="text" id="message" name="message" />
                <input id="sender" name="sender" value="<?= $_SESSION['user']['user_id']?>" style="display:none;"/>
                <input id="recipient" name="recipient" value="<?=$result->user_id?>" style="display:none;"/>
                  <input style="" id="submit" class="submit MsgInputHidden" type="submit" name="submit" value="Submit" />

                 </form>
            </div>
                     <script>
  $(function() {
    $(document).on('submit', 'form#SendForm<?=$result->user_id?>', function(e){
    e.preventDefault();
          $.post('message_send.php', $(this).serialize(), function (data) {
              // This is executed when the call to mail.php was succesful.
              // 'data' contains the response from the request
              $('#message').val(''); 
          })
          .error(function() {
              $('#message').val('');        
               });
          e.preventDefault();
                      $('#message').val(''); 
    })

  });
</script>
          <?php endwhile;?>

1 个答案:

答案 0 :(得分:0)

试试这个:

<?php $results = $con->query("SELECT * FROM tbl_users"); ?>
<?php while ($result = $results->fetch_object()): ?>
    <div class="chat" data-chat="person<?= $result->user_id ?>">
        <div class="conversation-start">
        </div>

        <?php
        $sender = $result->user_id;
        $messageload = $con->query("SELECT * FROM messages WHERE sender = $sender");
        ?>

        <?php while ($message = $messageload->fetch_object()): ?>
            <div class="bubble you">
                Hello,
            </div>
            <div class="bubble me">
                Are you serious?
            </div>
        <?php endwhile; ?>
    </div>

    <div class="write">
        <form autocomplete="off" id="SendForm<?= $result->user_id ?>" class="SendMsg" role="form" method="post" onsubmit="return add_message(this);">
            <input type="text" id="message" name="message" />
            <input id="sender" name="sender" value="<?= $_SESSION['user']['user_id'] ?>" style="display:none;"/>
            <input id="recipient" name="recipient" value="<?= $result->user_id ?>" style="display:none;"/>
            <input style="" id="submit" class="submit MsgInputHidden" type="submit" name="submit" value="Submit" />

        </form>
    </div>
<?php endwhile; ?>
<script>

    function add_message(form){
        $.post('message_send.php', $(form).serialize(), function (data) {
// This is executed when the call to mail.php was succesful.
// 'data' contains the response from the request
                $('#message').val('');
            }).error(function () {
                $('#message').val('');
            });
            $('#message').val('');
            return false;
    }


</script>