AJAX PHP评论框userid = 0?回复="&#34 ;?

时间:2015-03-25 12:40:50

标签: javascript php jquery mysql ajax

当我在textarea中输入文字时,它什么也没出现。 它显示:

  

cveUser name:jason123User id:1       forum_insert.js?t = 1427284551:44 ResponseText {“reply_id”:24,“user_id”:0,“reply”:“”,“username”:“jason123”,“profile_img”:“/ booking / images / user。 PNG“}

user_id和回复不起作用。

forum_insert.php

<?php

    if(isset($_POST['task']) && $_POST['task'] == 'forum_insert')
    {

        $user_id =(int) $_POST['user_id'];
        $reply =addslashes(str_replace("\n", "<br>" , $_POST['reply-text']));

        $std = new stdClass();
        $std->reply_id = 24;
        $std->user_id = $user_id;
        $std->reply = $reply;
        $std->username = "jason123";
        $std->profile_img = "/booking/images/user.png";

        echo json_encode($std);

    }
    else
    {
        header('Location: /booking/forumrm.php');
    }
?>

forum_insert.js

$(document).ready(function(){

$('#reply').click(function(){
    reply_click();
});
});

function reply_click(){

    //text within textarea which the person has entered.
    var text = $('#reply-text').val();
    var user_id = $('#user_id').val();
    var username = $('#username').val();




    if(text.length >0 && user_id != null)
    {
        $('.forum-insert-container').css('border','1px solid #e1e1e1' );
        console.log(text + "User name :" + username + "User id :" + user_id);

        $.post("/booking/forum_insert.php" ,
        {   
            task :"forum_insert",
            userId : user_id,
            reply : text
        }

        )

        .error(
            function()
            {
                console.log("Error");
            }
        )

        .success(

            function(data)
            {
                forum_insert(jQuery.parseJSON(data));
                console.log("ResponseText" +data);

            }
        );



    }
    else{
        $('.forum-insert-container').css('border','1px solid blue' );
        console.log("The text area was empty");
    }


    //remove text from textarea. 
    $('#reply-text').val("");
};      

function forum_insert(data)
{
    var t ='';
    t +='<li class="forum-holder" id="_'+data.reply_id+'">';                        
    t +='<div class="user-img">';
    t +='<img src="'+data.profile_img+'" class="user-img-pic" />';
    t +='</div>';

    t +='<div class="forum-body">';
    t +='<h3 class="username-field">'+data.username+'</h3>';

    t +='<div class="forum-text">'+data.reply+'</div>';
    t +=' </div>';

    t +='<div class="forum-buttons-holder">';
    t +='<ul>';
    t +='<li class="delete-btn">X</li>';
    t +='</ul>';
    t +='</div>';
    t +='</li>';

    $('.forum-holder-ul').prepend(t);

}

forum_box.php

<?php foreach($forum as $key => $forum ):?>

<li class="forum-holder" id="_1">

   <div class="user-img">
      <img src="images/user.png" class="user-img-pic" />
   </div>

            <div class="forum-body">
              <h3 class="username-field">
              jason123
              </h3>

            <div class="forum-text">
                            therdgdthbtrhdr
                             therdgdthbtrhdr
                        dthbtrhdrtherdgdthbtrhdr
                                 tdvgdhdfhfhherdgdthbtrhdr
                                 therdgdthbtrhdr
                                    therdgdthbtrhdr
            </div>
           </div>

        <div class="forum-buttons-holder">
     <ul>
        <li class="delete-btn">X</li>
     </ul>
   </div>
</li>
<?php endforeach; ?>

1 个答案:

答案 0 :(得分:0)

您正在使用userId而不是user_id

$.post("/booking/forum_insert.php" ,
        {   
            task :"forum_insert",
            userId : user_id,
            reply : text
        }

$user_id =(int) $_POST['user_id'];

将其更改为

$user_id =(int) $_POST['userId'];

相同
$reply =addslashes(str_replace("\n", "<br>" , $_POST['reply-text']));

$reply =addslashes(str_replace("\n", "<br>" , $_POST['reply']));