我无法让我的javascript函数工作?

时间:2011-12-01 00:02:18

标签: php javascript short

我无法运行onClick

的功能

我的问题是我无法让like_add工作,或者onclick不起作用,我不知道这就是为什么我需要帮助。

如果我提醒数据但我需要它来制作= .text(数据)

,它会起作用

TEST.php

<?php
//int
include '../scripts/connect_to_mysql.php';
session_start();
$_SESSION['user_id'] = '3';

//articles
function get_post() {
    $posts = array();

    $query = mysql_query("SELECT * FROM `post`");
    while (($row = mysql_fetch_assoc($query)) !== false) {
        $posts[] = array(
            'post_id' => $row['id'],
            'post_body' => $row['post_body'],
            'likes' => $row['likes']
        );
    }
    return $posts;
}

?>

<!doctype html>
<head>
<title>TEST</title>
<script type="text/javascript" src="js/jquery.min.js"></script>
<script type="text/javascript">
function like_add(post_id) {
$.ajax({
                type: "POST",
                url: "ajax/like_add.php",
                data: "post_id=" + post_id,
                success: 
                    function vote_get(post_id) {

                        $.ajax({
                                    type: "POST",
                                    url: "ajax/like_get.php",
                                    data: "post_id=" + post_id,
                                    success: function(data) { alert(data);  },
                                    error: function(msg){
                                        alert(msg);
                                      }             
                        });
                    },
                error: function(msg){
                    alert(msg);
                }
            });
}



</script>
</head>
<body>
<?php
    $posts = get_post();
    if (count($posts) == 0) {
        echo 'Sorry, there are no posts.';
    } else {
        echo '<ul>';
        foreach($posts as $post) {
            echo '<li><p>', $post['post_body'] ,'</p><p><a href="#" onclick="like_add('.$post['post_id'].')">Like</a>&nbsp;<span id="post_'. $post['post_id'].'_likes">', $post['likes'] ,'</span> people like this</p></li>';
        }
        echo '</ul>';

    }
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

试试这个:

<script type="text/javascript">
function like_add(post_id) {
$.ajax({
                type: "POST",
                url: "ajax/like_add.php",
                data: "post_id=" + post_id,
                success: function(post_id){
               vote_get(post_id);
               },

                error: function(msg){
                    alert(msg);
                }
            });
}
function vote_get(post_id) {

                        $.ajax({
                                    type: "POST",
                                    url: "ajax/like_get.php",
                                    data: "post_id=" + post_id,
                                    success: function(data) { $('#post_'+post_id+'_likes').text(data);  },
                                    error: function(msg){
                                        alert(msg);
                                      }             
                        });
                    }


</script>