douao2000 2011-12-01 00:02
浏览 14

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

I can't get the function to run onClick

my problem is I can't get like_add to work, or it's the onclick that doesn't work, I don't know thats why I need help.

It works if I alert data but I need it to make the = .text(data)

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条回答 默认 最新

  • dongyang0005 2011-12-01 00:57
    关注

    try this:

    <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>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 GDI处理通道视频时总是带有白色锯齿
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)
  • ¥15 自适应 AR 模型 参数估计Matlab程序
  • ¥100 角动量包络面如何用MATLAB绘制
  • ¥15 merge函数占用内存过大
  • ¥15 Revit2020下载问题
  • ¥15 使用EMD去噪处理RML2016数据集时候的原理
  • ¥15 神经网络预测均方误差很小 但是图像上看着差别太大
  • ¥15 单片机无法进入HAL_TIM_PWM_PulseFinishedCallback回调函数