jQuery没有正确输出返回的json数据

时间:2012-06-24 17:00:53

标签: jquery json

我正在为我的社交网络开发一堵墙。但它表现得非常奇怪。当我将第一个评论添加到新帖子时,它甚至不会使用jquery附加...但是一旦页面刷新它就会出现。这意味着它将它添加到数据库中。此外,当您添加新评论时,它会占用系统中已有的评论数量,并将新评论与有多少旧评论重复...我在下面有一个截图来描述这个,我的代码在下面。{{0} }

使用Javascript:

function addNewCommentData(defaultImgURI, firstname, lastname, returnedData, entryCreationDateTime) {
    return '<table cellpadding="0" cellspacing="0" style="width: 96%" class="style1 commentStyle">' + '<tr>' + '<td valign="top" style="width: 10px">' + '<img style="padding: 3px" id="defaultImg a0" src="' + defaultImgURI + '" align="left" width="25px" height="25px" />' + '</td>' + '<td valign="top" style="width: 319px">' + '<a class="font1 link-font1"><b>' + firstname + ' ' + lastname + '</b></a> ' + returnedData + '<br>' + entryCreationDateTime + '</td>' + '</tr>' + '</table>';
}

function addComment(e) {
    var userid = "<?=$this->session->userdata('userid')?>";
    var id = $(e).attr("id");
    var newId = id.replace("commentBox-", "");
    var commentBoxData = $("#commentBox-" + newId).val();
    $.ajax({
        type: "POST",
        dataType: "JSON",
        url: "<?=base_url()?>index.php/regUserDash/addComment",
        data: {
            commentBoxData: commentBoxData,
            userid: userid,
            newId: newId,
            commentAdded: true
        },
        success: function (data, id) {
            if (data.commentAdded === true) {
                var html = addNewCommentData(data.defaultImgURI, data.firstname, data.lastname, data.returnedData, data.entryCreationDateTime);
                e.closest('.status-border-bottom-box1').find('.commentsList').append(html);
            }
            else {
                return false;
            }
        }
    });
}

HTML:

<td id="main" valign="top" style="height: 111px; width: 506px">
<div id="scrollbar1">
<div class="scrollbar"><div class="track"><div class="thumb"><div class="end"></div></div></div></div>
<div class="viewport">
<div class="overview">
<?php   $this->load->helper('date');
        $userid = $this->session->userdata('userid');
        $query  = $this->db->query("SELECT * FROM churchMembers WHERE cMuserId = '{$userid}'");
        $row = $query->row();
        if ($query->num_rows() != 0) {
        $membersChurchId = $row->cMchurchId;
        $query = $this->db->query("SELECT wp.idwallPosts, wp.entryData, wp.entryCreationDateTime, u.firstname, u.lastname, u.userid, u.defaultImgURI FROM users u
                                   INNER JOIN wallPosts wp ON wp.postingUserid = u.userid
                                   WHERE wp.wpChurchId = '{$membersChurchId}' ORDER BY wp.idwallPosts DESC LIMIT 200");

        foreach ($query->result() as $row) { ?>
        <div id="new_posts"></div>
    <table id="load_status_out-<?=$row->idwallPosts?>" cellpadding="0" cellspacing="0" style="width: 376px; height: 63px" class="status-border-bottom-box1">
    <tr>
        <td valign="top" rowspan="4" style="padding: 4px; width: 465px">
        <img style="padding: 3px" id="defaultImg a0" src="<?php echo base_url().$row->defaultImgURI; ?>" width="59" height="64" />
        <a class="link-font2"><b>View Profile<br /></a><a class="delpost link-font2" delpost="<?=$row->idwallPosts?>" href="javascript:void(0)" id="delPost">Delete Post</a>
        </td>
        <td id="del" valign="top" style="width: 458px; height: 7px;">
        <a class="font1 link-font1 postname"><b><?php echo $row->firstname . " " . $row->lastname; ?></b></a>
        </td>
        <td valign="top" style="height: 7px; width: 41px">
        <!-- MSCellType="empty" -->
        </td>
    </tr>
    <tr>
        <td class="font1" valign="top" colspan="2" style="height: 40px">
        <?php echo $row->entryData; ?></td>
        </tr>
    <tr>
        <td valign="top" colspan="2">
        <a class="link-font1" id="like" href="#" style="width: 138px">Like</a>
        <span class="font2"> | </span>
        <a class="link-font1 commentLink" id="commentLink" rel="<?php echo $row->idwallPosts; ?>" href="javascript:void(0)" style="width: 138px">
        Comment</a>
        <span class="font2"> | <?=date('m/d/Y h:ia ', strtotime($row->entryCreationDateTime))?></span>
        </td>
    </tr>
    <tr>
        <td class="style1" valign="top" colspan="2" style="height: 42px">

        <div id="commentLikeListing" style="width: 95%; display: none"></div>
<?php   $query1 = $this->db->query("SELECT * FROM wallpostcomments wp INNER JOIN users u ON u.userid = wp.userid WHERE wallPostId = '{$row->idwallPosts}' ORDER BY wp.idwallPostComments ASC");
        foreach($query1->result() as $row1)
            if ($query->num_rows() > 0) { ?>
        <div id="likeList" style="display: none; width: 95%"></div>
        <div class="commentsList" style="width: 96%">
        <table cellpadding="0" cellspacing="0" style="width: 408px" class="style1 commentStyle">
        <tr>
        <td valign="top" style="width: 10px">
        <img style="padding: 3px" id="defaultImg a0" src="<?=base_url().$row1->defaultImgURI?>" align="left" width="25px" height="25px" />
        &nbsp;</td>
        <td valign="top" style="width: 319px">
        <a class="font1 link-font1"><b><?=$row1->firstname.' '.$row1->lastname?> </b></a><?=$row1->entryData.'<br>'.date('m/d/Y h:ia ', strtotime($row1->DateTimeCreated))?></a>
        </td>
    </tr>
</table>
        <div id="commentsListNewData" class="commentStyle" style="display: hidden; width: 96%"></div>
        <?php } ?>
        <input placeholder="Write a comment..." id="commentBox-<?php echo $row->idwallPosts; ?>" class="textbox1" style="width: 400px"></input>
        </td>
        </tr>
            </div>
</table>
<?php } } else { ?>
    <div class="font2" style="text-align: center">Sorry but you are not associated with any churches. Click <span class="link-font1" id="findChurch">here</span> to find your church.</div>
    <?php } ?>
        <div id="newData"></div></div>
</td>

有人知道发生了什么吗?

PHP代码(请记住这是CodeIgniter代码):

public function addComment() {
        $returnedEntryId = $this->input->post("newId");
        $returnedData    = $this->input->post("commentBoxData");
        $returnedUserId  = $this->input->post("userid");

        $this->db->query("INSERT IGNORE INTO wallPostComments (entryData, DateTimeCreated, userid, wallPostId)
                          VALUES('{$returnedData}', NOW(), '{$returnedUserId}', '{$returnedEntryId}')");

        $usersQuery1 = $this->db->query("SELECT * FROM users");
        foreach ($usersQuery1->result() as $row) {
            if ($row->userid == $returnedUserId) {
                $firstname      = $row->firstname;
                $lastname       = $row->lastname;
                $defaultImgURI  = base_url().$row->defaultImgURI;
                $entryCreationDateTime = 'succss'/*date('m/d/Y H:ia ', strtotime($time))*/;

        //$wallPostCommentQuery = $this->db->query("SELECT * FROM wallPotComments");

                echo json_encode(array('commentAdded' => true, 'defaultImgURI' => $defaultImgURI, 'firstname' => $firstname, 'lastname' => $lastname, 'returnedEntryId' => $returnedEntryId, 'returnedData' => $returnedData, 'returnedUserId' => $returnedUserId, 'entryCreationDateTime' => $entryCreationDateTime));
            }
        }
    }

0 个答案:

没有答案
相关问题