dss67853 2015-02-16 15:10
浏览 61
已采纳

如何在PHP中循环时为html div生成唯一ID

What I am trying to do is create a "save", "edit", and "hide: button for my website which applies to specific posts and comments, exactly like these buttons on Reddit. For now I am trying to self teach jQuery AJAX and attempting to figure out how to submit data to the database without having to reload the whole page. What I am attempting to do here is save a string by submitting it to a table called "Saved" when I click on "save", and edit, and hide the specific comment. However, the div id is static. I want to be able to make it dynamic when run through the loop so I can call the id selector in jQuery to complete the ajax functions for the specific comment. However, I cannot put php code into jQuery so calling the specific php $id variable is out of the question.

HTML and PHP5

<div id="main">
    <?php
        $comments = array("Message 1 is the first!", "Message 2 comes in second!", "This is the third message!");    

        for($i = 1; $i <= 3; $i++) {
            $id = $i;
            $comment = $comments[$i - 1];
            displayMsg($id, $comment);
        }
    ?>
    <form action="ajaxexample.php" method="post" style="display: none" id="1234">
        <input type="hidden" name="message" id="message" value="<?php echo $comment; ?>">
    </form> 
</div>
    </form>

PHP function

// Replace message1 with $id

function displayMsg($id, $comment){
    echo "<div id=\"message\" . $id ." style=\"border: solid 1px; width: 20%;\">";
    echo "<div id=\"pmessage\"><p>$comment</p></div>";
    echo "<a href=\"#\" class=\"Save\" style=\"color: blue;\">Save</a>";
    echo "<a href=\"#\" class=\"Edit\" style=\"color: blue;\">Edit</a>";
    echo "<a href=\"#\" class=\"Hide\" style=\"color: blue;\">Hide</a>";     
    echo "</div>";  
}

jQuery

$(document).ready(function () {
    $('a.Save').click(function () {

    });
    $('a.Hide').click(function () {
        $("#message" + $id).hide();
        $("#message"  + $id).fadeIn(2000);
    });
    $('a.Edit').click(function () {
        if ($(this).text() == "Edit") {
            $("#pmessage").css("color", "red");
            $("a.Edit").text("Unedit");
        } else {
            $("#pmessage").css("color", "black");
            $("a.Edit").text("Edit");
        }
    });

    $('a.Save').click(function (e) {
        e.preventDefault();
        $("#1234").submit();
    });

    $("#1234").submit(function(e) {
        $("a.Save").text("Saving...");
        e.preventDefault();
        $.ajax({
               type: "POST",
               url: 'ajaxexample.php',
               data: $("#1234").serialize(),
               success: function(data)
               {
                $("a.Save").text("Unsave");
               }
             });
    });
});
  • 写回答

2条回答 默认 最新

  • 普通网友 2015-02-16 16:25
    关注

    You can select the div parent instead :

    $('a.Hide').click(function () {
        $(this).parent().hide();
        $(this).parent().fadeIn(2000);
    });
    

    OR, you can add the attribute rel to your <a> like following :

    echo "<a href=\"#\" class=\"Edit\" rel=\"" . $id ."\" style=\"color: blue;\">Edit</a>";
    echo "<a href=\"#\" class=\"Hide\" rel=\"" . $id ."\" style=\"color: blue;\">Hide</a>";  
    

    And then :

    $('a.Hide').click(function () {
        var id = $(this).attr('rel');
        $('#message' + id).hide();
        $('#message' + id).fadeIn(2000);
    });
    

    For the edit button, change id="pmessage" to class="pmessage", the ID must be unique :

    $('a.Edit').click(function () {
        if ($(this).text() == "Edit") {
            $(this).parent().find('.pmessage').css("color", "red");
            $(this).text("Unedit"); // <-- HERE, use $(this) instead
        } else {
            $(this).parent().find('.pmessage').css("color", "black");
            $(this).text("Edit"); // <-- HERE, use $(this) instead
        }
    });
    

    About the save part :

    $('a.Save').click(function (e) {
        e.preventDefault();
        var $form = $(this).parent().find('form'),
            $this = $(this);
    
        $.ajax({
               type: $form.attr('method'),
               url: $form.attr('action'),
               data: $form.serialize(),
               beforeSend : function(){
                   $this.text("Saving...");
               },
               success: function(data)
               {
                  $this.text("Saved");
               },
               error: function(){
                  $this.text("Unsaved");
               }
             });
    });
    /*
    $("#1234").submit(function(e) {
        e.preventDefault();
        var $this = $(this);
        $.ajax({
               type: $this.attr('method'),
               url: $this.attr('action'),
               data: $this.serialize(),
               beforeSend : function(){
                   $("a.Save").text("Saving...");
               },
               success: function(data)
               {
                  $("a.Save").text("Saved");
               },
               error: function(){
                  $("a.Save").text("Unsaved");
               }
             });
    });
    */
    

    Create one form for each comment :

    function displayMsg($id, $comment){
        echo '<div id="message' . $id .'" style="border: solid 1px; width: 20%;">';
        echo '<div class="pmessage">'.$comment.'</div>';
        echo '<a href="#" rel="'.$id.'"  class="Save" style="color: blue;">Save</a>';
        echo '<a href="#" rel="'.$id.'" class="Edit" style="color: blue;">Edit</a>';
        echo '<a href="#" rel="'.$id.'" class="Hide" style="color: blue;">Hide</a>';
        echo '<form method="POST" action="mypage.php" style="display:none">';
        echo '<input type="text" name="message" value="'.$comment.'">';
        echo '</form>'
        echo '</div>';
    }
    

    NB : You need to change the a.save click statement.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 Centos7 / PETGEM
  • ¥15 csmar数据进行spss描述性统计分析
  • ¥15 各位请问平行检验趋势图这样要怎么调整?说标准差差异太大了
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 wpf界面一直接收PLC给过来的信号,导致UI界面操作起来会卡顿
  • ¥15 init i2c:2 freq:100000[MAIXPY]: find ov2640[MAIXPY]: find ov sensor是main文件哪里有问题吗
  • ¥15 运动想象脑电信号数据集.vhdr
  • ¥15 三因素重复测量数据R语句编写,不存在交互作用
  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗