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> <span id="post_'. $post['post_id'].'_likes">', $post['likes'] ,'</span> people like this</p></li>';
}
echo '</ul>';
}
?>
</body>
</html>