The Joomla! Forum ™



Forum rules


Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting, this means YOU.
Forum Post Assistant - If you are serious about wanting help, you will use this tool to help you post.



Post new topic Reply to topic  [ 3 posts ] 
Author Message
PostPosted: Sun Oct 30, 2011 1:24 pm 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Oct 30, 2011 1:20 pm
Posts: 3
i have included like.php into a article like this {jumi [like.php]} into joomla. and the contents of like.php is
Code:
<html>
   <head>
      <title>How to create a like/unlike button using jQuery and PHP</title>
       
   <script src="http://code.jquery.com/jquery-1.3.2.min.js"></script>
      <script>
         function doAction(postid,type){
            $.post('doAjax.php', {postid:postid, type:type}, function(data){
               if(isNaN(parseFloat(data))){
                  alert(data);
               }else{
                  $('#'+postid+'_'+type+'s').text(data);
               }
            });
         }
      </script>
   </head>
   <body>
      <?php
     
         include('db.php'); // including database connection.
         
         $postid = 1; // default post id
         $data = mysql_fetch_object(mysql_query('SELECT `like`,`unlike` FROM posts WHERE id="'.$postid.'"'));
     
      ?>
      <a href="javascript:;" onclick="doAction('<?php echo $postid;?>','like');">Like (<span id="<?php echo $postid;?>_likes"><?php echo $data->like;?></span>)</a>
      <a href="javascript:;" onclick="doAction('<?php echo $postid;?>','unlike');">Unlike (<span id="<?php echo $postid;?>_unlikes"><?php echo $data->unlike;?></span>)</a>
   </body>
</html>

db.php
Code:
<?php

   $host = 'localhost';
   $user = 'root';
   $pass = '';
   $db = 'like';
   
   $conn = mysql_connect($host, $user, $pass) or die(mysql_error());
   mysql_select_db($db, $conn) or die(mysql_error());
   
?>

ajax.php
Code:
<?php

   include('db.php');
   
   if($_POST['postid'] != '' && $_POST['type'] != ''){
     
      $alreadyExist = mysql_num_rows(mysql_query(' SELECT id FROM voted WHERE postid="'.(int)$_POST['postid'].'" AND ip="'.$_SERVER['REMOTE_ADDR'].'"'));
     
      if($alreadyExist==0){
         if($_POST['type']=='like'){
            mysql_query(' UPDATE posts SET `like`=`like`+1 WHERE id="'.(int)$_POST['postid'].'"');
            $num = mysql_fetch_row(mysql_query(' SELECT `like` FROM posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
         }elseif($_POST['type']=='unlike'){
            mysql_query(' UPDATE posts SET `unlike`=`unlike`+1 WHERE id="'.(int)$_POST['postid'].'"');
            $num = mysql_fetch_row(mysql_query(' SELECT `unlike` FROM posts WHERE id="'.(int)$_POST['postid'].'" LIMIT 1'));
         }
         echo $num[0];
         mysql_query(' INSERT INTO voted (`postid`,`ip`) VALUES ("'.(int)$_POST['postid'].'","'.$_SERVER['REMOTE_ADDR'].'")');
         
      }else{
         echo 'You already voted this.';
      }
     
   }

?>

codes taken from http://www.mlabs.info/create-likeunlike ... query-php/
but in my joomla page, it only previews the like and unlike button, i am unable to press it.
may i ask why ?


Top
 Profile  
 
PostPosted: Mon Oct 31, 2011 6:22 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Oct 30, 2011 1:20 pm
Posts: 3
anyone?


Top
 Profile  
 
PostPosted: Tue Nov 01, 2011 5:37 am 
Joomla! Fledgling
Joomla! Fledgling

Joined: Sun Oct 30, 2011 1:20 pm
Posts: 3
Could it be a javascript conflict with some other script in joomla site?


Top
 Profile  
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 



Who is online

Users browsing this forum: No registered users and 3 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  
Powered by phpBB® Forum Software © phpBB Group