Thursday 15 August 2013

javascript - AJAX- getting a specific array value in post -



javascript - AJAX- getting a specific array value in post -

im making scheme , encorporating ajax create smooth. works okay except defaults lastly post in loop. thinking there no way javascript know element of id "like" post to.

main.js:

$(".like>a").click(function() { $.post(base_url + "index.php/userprofile/like_post/", { post : post }, function(data) { alert('liked'); }, "json"); homecoming false; });

im passing through post variable view file. grab postid of each post.

userprofile_view.php:

<?php foreach ($posts $post) { ?> <?php $postid = $this->model_posts->getpostdata('id', $post->post); ?> <script type="text/javascript"> var post = "<?php echo $postid; ?>"; var base_url = "<?php echo base_url(); ?>"; </script>

model_posts.php:

function likepost($post) { $data['user_id'] = $this->session->userdata('id'); $data['post_liked'] = $post; $insert = $this->db->insert('user_post_likes', $data); homecoming $insert; }

userprofile.php(controller):

public function like_post() { $this->load->model('model_posts'); $post = $this->input->post('post'); $this->model_posts->likepost($post); }

if couldhelp me out great!

the problem usage of global variable in loop, variable have lastly value of loop.

you can utilize data-* attribute like

<script type="text/javascript"> var base_url = "<?php echo base_url(); ?>"; </script> <?php foreach ($posts $post) { ?> <?php $postid = $this->model_posts->getpostdata('id', $post->post); ?> <div class='posts'> <div class='posts_img'> <img src="<?php echo base_url() . 'img/profilepictures/thumbs/' . $profilepicture?>"> </div> <div class='posts_user'> <strong><?php echo $prefname; ?></strong> </div> <div class='posts_date'> <?php echo $this->model_posts->getposttime($post->post); ?> </div> <div class='post'> <p><?php echo $post->post ?></p> </div> <?php if($this->model_posts->doesuserlike($me, $postid)) { ?> <div class='unlike'> <?php echo anchor('userprofile/unlike_post/' . $me . '/' . $postid, 'unlike'); ?> </div> <?php } else { ?> <div class='like' data-post="<?php echo $postid; ?>"> <?php echo anchor('#', 'like', array('id' => 'like')); ?> </div> <?php } ?>

then

$(".like>a").click(function () { var post = $(this).parent().attr('data-post'); $.post(base_url + "index.php/userprofile/like_post/", { post: post }, function (data) { alert('liked'); }, "json"); homecoming false; });

javascript php jquery ajax

No comments:

Post a Comment