PHP Ajax在循环中提交始终插入循环

时间:2015-08-06 04:28:24

标签: php jquery ajax loops

我的PHP代码有问题,我在我的PHP While循环中放了一个AJAX来提交表单。但是当我按下输入时点击提交。查询插入了很多行。我希望在我的数据库中只插入一行。

我在while循环中的代码是:

$POST_ID = $GetData["POST_ID"];
<div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 0; padding: 0; margin-top: 15px;">
   <form action="profile/post.validation.php" method="POST" id="frmPostingComment-'.$POST_ID.'">
      <input type="hidden" id="GET_FLAG" name="GET_FLAG" value="POST_COMMENT">
      <input type="hidden" id="ENTITY_SOURCE" name="ENTITY_SOURCE" value="'.$ENTITY_SOURCE.'">
      <input type="hidden" id="PARENT_POST_ID" name="PARENT_POST_ID" value="'.$POST_ID.'">
      <input type="hidden" id="PARENT_ID" name="PARENT_ID" value="'.$GetData['MAIN_ID'].'">
      <input type="hidden" id="FULL_NM" name="FULL_NM" value="'.$vsFULL_NM_ACTV.'">
      <input type="hidden" id="USER_ID" name="USER_ID" value="'.$vnUSER_ID_ACTV.'">
      <textarea id="POSTING_COMMENT-'.$POST_ID.'" name="POSTING" class="form-control" style="border: 1px solid #ebebeb; resize: none; height: 40px;" placeholder="Write Comment"></textarea>
   </form>
</div>
</div>
</div>
<script>
   $("#POSTING_COMMENT-'.$POST_ID.'").keydown(function(event) {
    if (event.keyCode == 13) {
        $(this.form).submit()
        $(this).val("");
        return false;
     }
   }).focus(function(){
    if(this.value == ""){
         this.value = "";
         stopRefresh();
    }

   }).blur(function(){
    if(this.value==""){
         this.value = "";
         startRefresh();
    }
   });

   $("#frmPostingComment-'.$POST_ID.'").submit(function() {
   var formData = new FormData(this);

   $.ajax({
    type: "POST",
    url: $(this).attr("action"),
    data: formData,
    cache: true,
    contentType: false,
    processData: false,
    success: function(data) {
        $("#AJAX_POST_WRAPPER").load("profile/ajax.post-timeline.php?GetFLAG=HOME").fadeTo("fast", 1.0);
    },
      error: function(XMLHttpRequest, textStatus, errorThrown) {
          $("#AJAX_POST_WRAPPER").load("profile/ajax.post-timeline.php?GetFLAG=HOME").fadeTo("fast", 1.0);
      }
   })
    $("#POST_CONTENT").val("");
   return false;
   });

</script>

谢谢Stackoverflow! ;)

编辑:

<?PHP

    While ($GetData = MySQL_Fetch_Array($GetTIMELINE_POST)){

    $vnUSER_ID_TIMELINE = $GetData['USER_ID'];
    $vnPOST_ID          = $GetData['POST_ID'];

    $CheckingProfileTimeline =  MySQL_Query("SELECT * FROM ACCNT01USER WHERE USER_ID = '$vnUSER_ID_TIMELINE'");
                                IF(MySQL_Num_Rows($CheckingProfileTimeline)==1){
                                $ResultProfileTimeline=MySQL_Fetch_Array($CheckingProfileTimeline);}

    $CountingTimelineComment =  MySQL_Query("SELECT COUNT(*) AS TOTAL_COMMENT FROM ACCNT01POST_REL WHERE PARENT_POST_ID = '$vnPOST_ID'");
                                IF(MySQL_Num_Rows($CountingTimelineComment)==1){
                                $ResultCountingTimelineComment=MySQL_Fetch_Array($CountingTimelineComment);}

                                $vnTOTAL_COMMENT = $ResultCountingTimelineComment['TOTAL_COMMENT'];

    Print '
        <li>
            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin-bottom: 40px;">
                <div class="col-lg-3 col-md-3 col-sm-3 col-xs-3">
                    <a href="profile.php?username='.$ResultProfileTimeline['USER_NM'].'">
                    <div class="avatar">
                        <img src="storages/avatar/'.$ResultProfileTimeline['AVATAR'].'">
                    </div>
                    </a>

                    '; IF($GetData['POST_TYPE'] == 'TEXT'){ Print '

                        <div class="avatar-badges badges-texts">
                            <img src="assets/storages/component/SVG/cd-icon-text.svg">
                        </div>

                    '; }Else{ Print '

                        <div class="avatar-badges badges-pictures">
                            <img src="assets/storages/component/SVG/cd-icon-picture.svg">
                        </div>

                    '; } Print '


                </div>

                <div class="col-lg-9 col-md-9 col-sm-9 col-xs-9">
                    <div class="bubble-container" style="width: 100%; ,ar">
                        <div class="bubble" style="margin-top: -20px; text-light">
                            <div class="title-wrapper" style="margin-top: -20px;">
                                <a href="profile.php?username='.$ResultProfileTimeline['USER_NM'].'" class="link-negative"><h4 class="text-semibold">'.$ResultProfileTimeline['FULL_NM'].'  </a> <span class="text-muted text-light"> @'.$ResultProfileTimeline['USER_NM'].'</span> '; IF($ResultProfileTimeline['USER_LEVEL'] == 'VERIFIED'){ Print '  <span class="label animated infinite rubberBand" data-toggle="tooltip" data-placement="top" title="Verified Account" style="position:relative; background: #3296dc; border-radius: 50%; top:-10px; height: 20px; width: 20px; font-size:10px; border: 2px solid white;"><i class="fa fa-check" style="top: 1px; left: -3px; position: relative;"></i></span> '; } Print' <span class="text-light" style="float: right; font-size: 12px; position: relative; left: 10px;">'; Print TimingElapsed($GetData['REG_DT']); Print' '; setCascadeClause($ResultProfileTimeline['USER_NM'], $gsUSER_NM, 'IF_SAME', '- <span class="text-light" style="float: right; font-size: 12px;"><form action="profile/post.validation.php" id="frmDeletePostComment" method="POST" ><input type="hidden" id="POST_ID" name="POST_ID" value="'.$GetData['POST_ID'].'"> <input type="hidden" id="GET_FLAG" name="GET_FLAG" value="DELETE_STATUS"> <input type="hidden" id="ENTITY_SOURCE" name="ENTITY_SOURCE" value="'.$ENTITY_SOURCE.'"> <button type="submit" class="btn btn-default btn-sm" style="background: transparent; border: none; margin-top: -4px; margin-left: -8px;"><i class="fa fa-trash"></i></button></form></span>', '');  Print'</span> </h4>
                                <span class="separator separator-black separator-lg-light" style="margin-top: 10px; margin-bottom: -10px; color: #e6e6e6; background: grey; "></span>
                            </div>

                            <br/>

                            '; 

                            $StickerMaps = [
                                ":)"    =>  "<img class='emoticon' src='assets/storages/component/stickers/smile.svg' style='height: 40px; width: 40px;' title='Smile :)' />",
                                ":))"   =>  "<img class='emoticon' src='assets/storages/component/stickers/smiles.svg' style='height: 40px; width: 40px;' title='Smiles :))' />",
                                ":D"    =>  "<img class='emoticon' src='assets/storages/component/stickers/laugh.svg' style='height: 40px; width: 40px;' title='Laugh :D' />",
                                ";)"    =>  "<img class='emoticon' src='assets/storages/component/stickers/clink.svg' style='height: 40px; width: 40px;' title='Clink ;)' />",
                                ":DD"   =>  "<img class='emoticon' src='assets/storages/component/stickers/laughover.svg' style='height: 40px; width: 40px;' title='Laughover :DD' />",
                                ":P"    =>  "<img class='emoticon' src='assets/storages/component/stickers/tongue.svg' style='height: 40px; width: 40px;' title='Tongue :P' />",
                                ":-P"   =>  "<img class='emoticon' src='assets/storages/component/stickers/tongues.svg' style='height: 40px; width: 40px;' title='Tongues :-P' />",
                                "B)"    =>  "<img class='emoticon' src='assets/storages/component/stickers/cool.svg' style='height: 40px; width: 40px;' title='Cool B)' />",
                                ":S)"   =>  "<img class='emoticon' src='assets/storages/component/stickers/shy.svg' style='height: 40px; width: 40px;' title='Shy :S)' />",
                                ":-S)"  =>  "<img class='emoticon' src='assets/storages/component/stickers/shys.svg' style='height: 40px; width: 40px;' title='Shy :-S)' />",
                                "<3"    =>  "<img class='emoticon' src='assets/storages/component/stickers/love.svg' style='height: 40px; width: 40px;' title='Feeling in Love <3' />",
                                ":-D"   =>  "<img class='emoticon' src='assets/storages/component/stickers/happy.svg' style='height: 40px; width: 40px;' title='Happy :-D' />"
                            ];

                            $POST_CONTENT       = pregReplacing($GetData['POST_CONTENT']);
                            $POST_CONTENT_APPLY = strtr($POST_CONTENT, $StickerMaps);   

                            $POST_ID = $GetData["POST_ID"];

                            Print'

                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 0; padding: 0; margin-bottom: 10px;">'.$POST_CONTENT_APPLY.'</div>

                            '; IF($GetData['POST_TYPE'] == 'IMAGES'){ Print'

                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="padding: 15px;">
                                    <img src="storages/attachment/'.$GetData['POST_ATTACHMENT'].'" class="img-responsive">
                                </div>

                            '; }Else{ } Print'

                            <div class="col-lg-3 col-md-1 col-sm-1 col-xs-6" style="margin-top: 30px; margin: 0; padding: 0;">
                                <a href="#" class="link-negative" style="background: transparent; border: 0; margin: 0; padding: 0;"> <i class="fa fa-comments-o text-muted"></i> &nbsp;Comment '; IF($vnTOTAL_COMMENT == 0){ Print ''; }Else{ Print '('.$vnTOTAL_COMMENT.')'; } Print' </a>                                
                            </div>

                            <div class="col-lg-3 col-md-2 col-sm-2 col-xs-6" style="margin-top: 30px;  margin: 0; padding: 0;">
                                <a href="#" class="link-negative" style="background: transparent; border: 0; margin: 0; padding: 0;"> <i class="fa fa-reply text-muted"></i> &nbsp;Reshare </a>                             
                            </div>

                            <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 0; padding: 0;" id="AJAX_POST_COMMENT_WRAPPER" name="AJAX_POST_COMMENT_WRAPPER">
                                <span class="separator separator-black separator-lg-light" style="margin-top: 10px; margin-bottom: -10px; color: #e6e6e6; background: grey; "></span>

                                ';  $GetTIMELINE_COMMENT  = MySQL_Query("SELECT A.*
                                                                         FROM ACCNT01POST_REL A
                                                                         WHERE  A.PARENT_POST_ID = $POST_ID
                                                                         ORDER  BY A.POST_ID ASC
                                                                         LIMIT 22;")
                                                            OR DIE(MySQL_ERROR());

                                    While ($GetDataComment = MySQL_Fetch_Array($GetTIMELINE_COMMENT)){ 

                                    $vnUSER_ID_TIMELINE_COMMENT = $GetDataComment['USER_ID'];

                                    $CheckingProfileTimelineComment =  MySQL_Query("SELECT * FROM ACCNT01USER WHERE USER_ID = '$vnUSER_ID_TIMELINE_COMMENT'");
                                    IF(MySQL_Num_Rows($CheckingProfileTimelineComment)==1){
                                    $ResultProfileTimelineComment=MySQL_Fetch_Array($CheckingProfileTimelineComment);}

                                    Print '

                                    <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 0; padding: 0;">
                                        <div class="col-lg-2 col-md-2 col-sm-2 col-xs-2" style="margin-top: 18px; margin-left: -15px;">
                                            <img src="storages/avatar/'.$ResultProfileTimelineComment['AVATAR'].'" class="img-responsive img-circle" style="width: 50px; height: 50px;">
                                        </div>

                                        <div class="col-lg-10 col-md-10 col-sm-10 col-xs-10" style="margin-top: 10px; margin-left: -15px;">
                                            <h5 class="text-semibold text-muted"> '.$ResultProfileTimelineComment['FULL_NM'].' <span class="text-light">@'.$ResultProfileTimelineComment['USER_NM'].'</span>  <span class="text-light" style="margin-right: -30px; float: right;"> '; Print TimingElapsed($GetDataComment['REG_DT']);  setCascadeClause($ResultProfileTimeline['USER_NM'], $gsUSER_NM, 'IF_SAME', '- <span class="text-light" style="float: right; font-size: 12px;"><form action="profile/post.validation.php" id="frmDeletePostStatus" method="POST" ><input type="hidden" id="POST_ID" name="POST_ID" value="'.$GetDataComment['POST_ID'].'"> <input type="hidden" id="GET_FLAG" name="GET_FLAG" value="DELETE_COMMENT"> <input type="hidden" id="ENTITY_SOURCE" name="ENTITY_SOURCE" value="'.$ENTITY_SOURCE.'"> <button type="submit" class="btn btn-default btn-sm text-muted" style="background: transparent; border: none; margin-top: -4px; margin-left: -8px;"><i class="fa fa-trash text-mutedd"></i></button></form></span>', ''); setCascadeClause($ResultProfileTimelineComment['USER_NM'], $gsUSER_NM, 'IF_SAME', '&nbsp; <span class="text-light" style="float: right; font-size: 12px;"><form action="profile/post.validation.php" id="frmDeletePostStatus" method="POST" ><input type="hidden" id="POST_ID" name="POST_ID" value="'.$GetDataComment['POST_ID'].'"> <input type="hidden" id="GET_FLAG" name="GET_FLAG" value="DELETE_COMMENT"> <input type="hidden" id="ENTITY_SOURCE" name="ENTITY_SOURCE" value="'.$ENTITY_SOURCE.'"> <button type="submit" class="btn btn-default btn-sm text-muted" style="background: transparent; border: none; margin-top: -4px; margin-left: -8px; margin-right: -50px;"><i class="fa fa-trash text-muted"></i></button></form></span>', ''); Print'  </span> </h5>

                                             ';

                                             $POST_CONTENT_COMMENT       = pregReplacing($GetDataComment['POST_CONTENT']);
                                             $POST_CONTENT_COMMENT_APPLY = strtr($POST_CONTENT_COMMENT, $StickerMaps);  

                                             Print $POST_CONTENT_COMMENT_APPLY;

                                             Print '
                                        </div>
                                    </div>

                                '; } Print'

                                <div class="col-lg-12 col-md-12 col-sm-12 col-xs-12" style="margin: 0; padding: 0; margin-top: 15px;">
                                    <form action="profile/post.validation.php" method="POST" id="frmPostingComment-'.$POST_ID.'">
                                        <input type="hidden" id="GET_FLAG" name="GET_FLAG" value="POST_COMMENT">
                                        <input type="hidden" id="ENTITY_SOURCE" name="ENTITY_SOURCE" value="'.$ENTITY_SOURCE.'">

                                        <input type="hidden" id="PARENT_POST_ID" name="PARENT_POST_ID" value="'.$POST_ID.'">
                                        <input type="hidden" id="PARENT_ID" name="PARENT_ID" value="'.$GetData['MAIN_ID'].'">

                                        <input type="hidden" id="FULL_NM" name="FULL_NM" value="'.$vsFULL_NM_ACTV.'">
                                        <input type="hidden" id="USER_ID" name="USER_ID" value="'.$vnUSER_ID_ACTV.'">

                                        <textarea id="POSTING_COMMENT-'.$POST_ID.'" name="POSTING" class="form-control" style="border: 1px solid #ebebeb; resize: none; height: 40px;" placeholder="Write Comment"></textarea>
                                    </form>
                                </div>
                            </div>

                        </div>

                        <script>

                        $("#POSTING_COMMENT-'.$POST_ID.'").keydown(function(event) {
                            if (event.keyCode == 13) {
                                $(this.form).submit()
                                $(this).val("");
                                return false;
                             }
                        }).focus(function(){
                            if(this.value == ""){
                                 this.value = "";
                                 stopRefresh();
                            }

                        }).blur(function(){
                            if(this.value==""){
                                 this.value = "";
                                 startRefresh();
                            }
                        });

                        $("#frmPostingComment-'.$POST_ID.'").submit(function() {
                        var formData = new FormData(this);

                        $.ajax({
                            type: "POST",
                            url: $(this).attr("action"),
                            data: formData,
                            cache: true,
                            contentType: false,
                            processData: false,
                            success: function(data) {
                                $("#AJAX_POST_WRAPPER").load("profile/ajax.post-timeline.php?GetFLAG=HOME").fadeTo("fast", 1.0);
                            },
                              error: function(XMLHttpRequest, textStatus, errorThrown) {
                                  $("#AJAX_POST_WRAPPER").load("profile/ajax.post-timeline.php?GetFLAG=HOME").fadeTo("fast", 1.0);
                              }
                        })
                         $("#POST_CONTENT").val("");
                        return false;
                    });

                        </script>

                        <div class="arrow"></div>
                    </div>
                </div>
            </div>
        </li>   
    '; }

?>

0 个答案:

没有答案