0

After a lot googling i acheived to send a request to add as friend, say user a sends a friend request to user b and user b logs in he will see the request from user a ... say only his name is displayed when the user clicks on that name a dialog box is opened which will ask the user whether to accpet or reject that request, the problem i am facing right now is i am not able to find how can i update the friends table status column from pending to accepted i.e when the alert message Friend request accepted is displayed the status column of friends table should be updated from pending to accepted and if rejected it should update the status column as rejected

please guide me through this

<script>

        $(function(){
            $("#shortthemes a").click(function(e){
                e.preventDefault();
                $("link#theme").attr('href',$(this).attr('href'));
                $("#shortthemes a").removeClass('selected');
                $(this).addClass('selected');
            });
        });


        function tstconfirm(){
            smoke.confirm('Confirm as Friend !',function(e){
                if (e){
                    alert('Friend Request Accepted');

                }else{
                    alert('Friend Request Rejected');
                }
            });
        }

</script>
Tarik
  • 79,711
  • 83
  • 236
  • 349
user760844
  • 17
  • 1

1 Answers1

0

Do a post to a PHP-file which handles your Post-request and updates the database:

  <script>

    $(function(){
        $("#shortthemes a").click(function(e){
            e.preventDefault();
            $("link#theme").attr('href',$(this).attr('href'));
            $("#shortthemes a").removeClass('selected');
            $(this).addClass('selected');
        });
    });


    function tstconfirm(){
        smoke.confirm('Confirm as Friend !',function(e){
            if (e){
                alert('Friend Request Accepted');
                $.post('friend_actions.php', {pk_friendrequest_id: fk_partner_id.val(), action: 'accept_request'}, function(data){  if (data.length > 100) { alert(data); } else { window.location.replace(data) }; });

            }else{
                alert('Friend Request Rejected');
                $.post('friend_actions.php', {pk_friendrequest_id: fk_partner_id.val(), action: 'reject_request'}, function(data){  if (data.length > 100) { alert(data); } else { window.location.replace(data) }; });

            }
        });
    }

In your friend_actions.php add:

    if ($_POST['action'] == 'accept_request') {
     $qry = "UPDATE friend_request SET status = 'Accepted' WHERE pk_friendrequest_id = '.$_POST['pk_friendrequest_id'];

     $db = mysql_connect('YOUR_DB_ADDRESS','YOUR_DB_USER','YOUR_DB_PASS') or die("Database error");
     mysql_select_db('YOUR_DB', $db);

     mysql_query($qry);     

echo "index.php"; //Or any other page where you want the user to go after completion
    }

    if ($_POST['action'] == 'reject_request') {
     $qry = "UPDATE friend_request SET status = 'Rejected' WHERE pk_friendrequest_id = '.$_POST['pk_friendrequest_id'];

     $db = mysql_connect('YOUR_DB_ADDRESS','YOUR_DB_USER','YOUR_DB_PASS') or die("Database error");
     mysql_select_db('YOUR_DB', $db);

     mysql_query($qry);     

echo "index.php"; //Or any other page where you want the user to go after completion
    }

The PHP-file returns a URL. The Javascript checks if the response is <100 characters (else it's probably an error and it alerts it for debugging) and if so, it redirects you to the URL.

Rick
  • 181
  • 13
  • dude my sql schema for friends table contains user_id friend_id status say for e.g user_id = 1 friend_id = 4 status = pending then according to u pk_friendrequest_id: fk_partner_id.val() should it be changed to pk_friendrequest_id: user_id.val() ? if so i made this changes but it is not updating my friends table status to accepted when a user clicks on ok or rather when user clicks cancel it doesnot update it to rejected. – user760844 Jul 12 '11 at 06:18
  • First of all, this is an example of course. You need to change all the column names to your own names. The fk_partner_id is a column from one of my own scripts, where I copied this from. I forgot to change the name. With this example, I tried to give you a push in the right direction. I showed you how to use Jquery to push values from a form to a PHP script. It's up to you to fill in your own details. – Rick Jul 12 '11 at 08:25