1

I am working on a social networking site where user Posts are displayed on the home page. They can be liked and commented on. If a post is liked, it updates the like table through AJAX and have like count incremented by one.

AJAX code:

$(".mlike").click(function () {
        $(".murconform").submit(function(e){
            return false;
        });
        var $this=$(this);
        var post_id = $(this).val();
        var user_id = $(".user_id").text();
        alert('Post: '+post_id +' User: '+user_id);
        var request = $.ajax({
          url: "likes.php",
          type: "POST",
          data: { post : post_id , user : user_id },
          dataType: "html"
        });
        request.done(function( msg ) {    
            $this.prev('.likecount').html( msg ); 
        });
});

In the home.php page I have some PHP variables ($userID, $userName) that are data fetched from MySQL and they all work fine but they don't work with the variables ($viewedUserID, $viewedUserName) in the user.php. In the user.php, only posts related to profile been viewed are fetched but when you press the like button and try to comment on any of the post it says undefine variables; $viewedUserID, $viewedUserName. And these variables are defined from the beginning of the page in user.php.

I have been thinking of what might be the possible cause of this and was also thinking the AJAX was suppossed to have effect on the clicked button only.

NOTE: The alert works just fine.

Thanks in advance.

Yax
  • 2,127
  • 5
  • 27
  • 53
  • 1
    cannot locate `$viewedUserID` or `$viewedUserName` in your code. – bansi Jan 18 '14 at 09:32
  • @bansi: They are in `user.php` will be too long to show here. Okay, say `$viewedUserID = 1; and $viewedUserName = 'Yax';` – Yax Jan 18 '14 at 09:36
  • Are the variables accessible in likes.php? – Nouphal.M Jan 18 '14 at 09:45
  • @Nouphal.M They are not needed in `likes.php`. All that is needed is the `post_id and user_id` to update the **likes table** properly. – Yax Jan 18 '14 at 09:49
  • confusing... totally unclear what you are trying to do. How can you get the variables declared in `user.php` at `likes.php`? You may have to store it in session, include user.php or post it from the `user.php` page. – bansi Jan 18 '14 at 10:00
  • @bansi: `likes.php` is called via AJAX and the only thing it returns is the total number of counts for the post that is liked. Those variables are declared and used in their respective pages which is totally independent of `likes.php`. – Yax Jan 18 '14 at 10:09
  • So what is your actual problem? – Nouphal.M Jan 18 '14 at 10:12
  • if you defined $viewedUserID and $viewedUserName out of the function, don't forget to call them via $this, $this->$viewedUserName ... – أنيس بوهاشم Jan 18 '14 at 10:18
  • @Nouphal.M: AJAX doesn't update the update the **like table** even though `user_id and post_id` that are required are available. And the page returns `PHP` error: saying `Undefine variables` $viewedUserID, $viewedUserName. – Yax Jan 18 '14 at 10:19

1 Answers1

0

Was going to write as a comment, but I guess an answer will be clearer:

Ajax

How does AJAX work?

I think you're getting confused with what Ajax's job is. The problem is Ajax is literally just a connector between your front-end (JS / HTML) and back-end (PHP / Rails etc)

Your statements that "Ajax isn't updating MYSQL" lead me to believe you're relying on Ajax to update your table. It won't

Your table will update by using PHP, which is why most of the comments are focused on the PHP & not the JS

PHP

Your Ajax needs to send the correct data to PHP, but then it's your server-side scripts' job to sort it all out, sending a worthy response

Your JS looks like it will work well, but I think your problem will be with your backend. If you update your question with your PHP you'll get a lot more clearer answers!

Community
  • 1
  • 1
Richard Peck
  • 76,116
  • 9
  • 93
  • 147
  • The **likes.php** has a query that insert post_id, user_id, date and other related data into like table. Likes.php only uses two POSTED data which are the post and user ID. If I click on a like button in **home.php** everything works fine but PHP returns **Undefined variables** for all the variables in **user.php**. – Yax Jan 18 '14 at 11:55
  • How are you passing the variables to **user.php**? – Richard Peck Jan 18 '14 at 12:40
  • Just realised that AJAX is returning whole page. I mean the requesting page. – Yax Jan 18 '14 at 17:48
  • 1
    So it kind of works, except it's returning the requesting page? What are the contents on the returned page? I'm really interested to hear!!!! – Richard Peck Jan 18 '14 at 17:59
  • It returns the HTML of the requesting with PHP errors in it. I am trying to post another question. – Yax Jan 18 '14 at 18:39
  • Have just asked. Please help me look at it. Thanks! – Yax Jan 18 '14 at 19:12