为dataString设置变量

时间:2016-10-24 18:49:44

标签: php jquery ajax get

我正在创建一个用户评论系统。现在我正在尝试SELECT用户的最后评论,然后自动使用AJAX获取新评论。

我收到的错误是dataString未定义。我不知道如何在我的PHP中定义这个。因为我回应了所有这些:

echo '<div class="comment-post-box">';
                echo $commenter_img;
                echo '<div class="comment-post-username">'.$comment_username. '</div>';
                echo '<div>'.$comment_date. '</div>';
                echo '<div class="comment-post-text">'.$home_comments. '</div>';
                echo '</div>';

我只是将所有这些设置为dataString或我该怎么办?

完整代码:

这是我在评论页面上的表单和SELECT查询。这样,结果将加载到页面加载。

<form action="" method="POST" id="comment-form">
            <textarea id="home_comment" name="comment" placeholder="Write a comment..." maxlength="1000" required></textarea><br>
            <input type="hidden" name="token" value="<?php echo Token::generate(); ?>">
            <input id="comment-button" name="submit" type="submit" value="Post">
        </form>
        <div id="comment-container">
<?php
$select_comments_sql = "
    SELECT c. *, p.user_id, p.img
    FROM home_comments AS c
    INNER JOIN (SELECT max(id) as id, user_id 
                FROM profile_img 
                GROUP BY user_id) PI
      on PI.user_id = c.user_id
    INNER JOIN profile_img p
      on PI.user_id = p.user_id
     and PI.id = p.id
    ORDER BY c.id DESC
";

  if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
        $select_comments_stmt->execute();
        if (!$select_comments_stmt->errno) {
            //echo "error";
        }
        $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date, $commenter_user_id, $commenter_img);
    //var_dump($select_comments_stmt);  
        $comment_array = array();
        while ($select_comments_stmt->fetch()) {
            $comment_array[] = $comment_user_id;
            $comment_array[] = $comment_username;
            $comment_array[] = $home_comments;
            $comment_array[] = $comment_date;
            $comment_array[] = $commenter_user_id;
            $comment_array[] = $commenter_img;
            $commenter_img = '<img class="home-comment-profile-pic" src=" '.$commenter_img.'">';
            if ($home_comments === NULL) {
                echo 'No comments found.';
            } else {
                echo '<div class="comment-post-box">';
                echo $commenter_img;
                echo '<div class="comment-post-username">'.$comment_username. '</div>';
                echo '<div>'.$comment_date. '</div>';
                echo '<div class="comment-post-text">'.$home_comments. '</div>';
                echo '</div>';
            }
        }
  }

AJAX文件

function commentRetrieve(){

        $.ajax({ 
                url: "ajax-php/comment-retrieve.php",
                type: "get",
                data: dataString,
                success: function (data) {
                //  console.log(data);
                    if (data == "Error!") {
                        alert("Unable to retrieve comment!");
                        alert(data);
                    } else {
                        $('#comment-container').prepend(data);
                    }
                },
                error: function (xhr, textStatus, errorThrown) {
                    alert(textStatus + " | " + errorThrown);
                    console.log("error"); //otherwise error if status code is other than 200.
                }
            });


    }
    setInterval(commentRetrieve, 300);

PHP文件

$user = new User();

function selectNewComment() {

//Get the last insert id
    $select_comments_sql = "
    SELECT c. *, p.user_id, p.img
    FROM home_comments AS c
    WHERE c.id=$last_id
    INNER JOIN (SELECT max(id) as id, user_id 
                FROM profile_img 
                GROUP BY user_id) PI
      on PI.user_id = c.user_id
    INNER JOIN profile_img p
      on PI.user_id = p.user_id
     and PI.id = p.id
    ORDER BY c.id DESC
";

  if ($select_comments_stmt = $con->prepare($select_comments_sql)) {
        $select_comments_stmt->execute();
        if (!$select_comments_stmt->errno) {
            //echo "error";
        }
        $select_comments_stmt->bind_result($comment_id, $comment_user_id, $comment_username, $home_comments, $comment_date, $commenter_user_id, $commenter_img);    
        $comment_array = array();
        while ($select_comments_stmt->fetch()) {
            $comment_array[] = $comment_user_id;
            $comment_array[] = $comment_username;
            $comment_array[] = $home_comments;
            $comment_array[] = $comment_date;
            $comment_array[] = $commenter_user_id;
            $comment_array[] = $commenter_img;
            $commenter_img = '<img class="home-comment-profile-pic" src=" '.$commenter_img.'">';
            if ($home_comments === NULL) {
                echo 'No comments found.';
            } else {
                echo '<div class="comment-post-box">';
                echo $commenter_img;
                echo '<div class="comment-post-username">'.$comment_username. '</div>';
                echo '<div>'.$comment_date. '</div>';
                echo '<div class="comment-post-text">'.$home_comments. '</div>';
                echo '</div>';
            }
        }
    }
}
}   

selectNewComment();

$ con variable error:

我的初始文件:

$servername = 'localhost';
$usernameCon = 'actual';
$passwordCon = 'actual';

try {   
    $con = new PDO('mysql:host='.$servername.';dbname=actual', $usernameCon, $passwordCon);
    $con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e)
    {
    echo "Connection failed: " . $e->getMessage();
    }
    $user_id = ( isset( $_SESSION['user'] ) ? $_SESSION['user'] : "" );

1 个答案:

答案 0 :(得分:0)

您收到此错误,因为未定义javascript变量dataString:)

你是如何调用javascript函数的?当您通过ajax调用页面时,是否需要指定任何数据?

解决方案 - 指定ID(例如)

$.ajax({ 
  url: 'ajax-php/comment-retrieve.php',
  type: 'get',
  data: {id: 36}, // for example, id #36. You can retrieve this data on your PHP page via $_GET['id']
  success: function (data) {
    /* [...] rest of your code */
  }
});

另一种解决方案 - 删除data / dataString

$.ajax({ 
  url: 'ajax-php/comment-retrieve.php',
  type: 'get',
  success: function (data) {
    /* [...] rest of your code */
  }
});

I.E。:我在$last_id中看到$select_comments_sql变量。我认为还有另一个问题:)