3

Ive been trying to get this for age

I am building a website which has an activity wall. I have the whole thing working except the like and unlike buttons. I have them currently just showing a text box I like or I don't like

<a href='#' onclick='like()'>Like</a>

or

<a href='#' onclick='unlike()'>Unlike</a>

Now these call these scripts

<script>
    function like()
    {
        alert("I like");
        document.getElementById("p1").innerHTML="<a href='#' onclick='unlike();'>Unlike</a> | 1 Life<hr/>";
    }
    function unlike()
    {
        alert("Dont like anymore");
        document.getElementById("p1").innerHTML="<a href='#' onclick='like();'>Like</a> | 0 Lifes<hr/>";
    }
</script>

I have a php script that connects to the database and adds or removes the like which I know works. I don't need to return anything to the javascript but how do I call that php script giving the postID and username??


Ok so using this I have modified it a little. But it still doesnt work :S

==LikeUnlink.php==

<?php
include 'phpScripts/OpenConnection.php';    
$mode = $_GET['mode'];
$username = $_GET['username'];
$postID = $_GET['LikeID'];
echo $username."<br/>";
echo $mode."<br/>";
echo $postID."<br/>";
if($mode == 0)
{
    $query = "INSERT INTO tbl1Ups (Username, ActivityID) VALUES ('$username', $postID)";    
}
else
{
    $query = "DELETE FROM `tbl1Ups` WHERE Username='$username' AND ActivityID=$postID";
}
$result = mysql_query($query) or die(mysql_error());

==MembersHome.php==

<script type="text/javascript">
function process(LikeId, Username, currentLikes, likeUnlike)
{
//your validation code
    $.ajax(
    {
    type: 'GET',
        url: LikeUnlike.php, //file where like unlike status change in database
    data:{like:LikeId, username:Username, mode:likeUnlike},
    success: function(data)
        {
            alert('It Works');
    }
    } );
}
</script>

==Like link==

<a href='Javascript:void(0)' onclick='process(".$row['ID'].", \"".$username."\", ".$likes.", 0);'>$linkText</a>
5
  • Do u want to pass data from the js to php ? and php will pass the data to the db ? Commented May 1, 2013 at 6:10
  • 1
    An AJAX call is probably best... If you don't want it to return anything, then you can just return some garbage and ignore it. Commented May 1, 2013 at 6:14
  • you need something like ajax stackoverflow.com/questions/12026600/… Commented May 1, 2013 at 6:34
  • you should call ajax for implement this Commented May 1, 2013 at 6:53
  • 1
    Yeah I want the js -> php -> database Commented May 1, 2013 at 10:43

1 Answer 1

3

try this

<script type="text/javascript">
function process(LikeId) { 
//your validation code
$.ajax( {
        type: 'POST',
        url: LikeUnlike.php, //file where like unlike status change in database
        data:{like:LikeId},
        success: function(data) {
            //code you want to do after successfull process
        }
    } );
}
</script>

make changes in html

<a href='Javascript:void(0)' onclick='process(1)'>Like</a>
<a href='Javascript:void(0)' onclick='process(0)'>Unlike</a>
Sign up to request clarification or add additional context in comments.

4 Comments

You might want to do some validation to make sure it was actually successful instead of assuming that a successful AJAX call was a successful like. That's up to the OP though.
@Jared yes that can be checked through the response of LikeUnlike.php i just give the way he can do... i should not give all the code right?
Yeah the server part of it is for another question if OP can't do it themselves.
So am I right in saying to post to the php file I would use data:{}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.