0

i am having some trouble with some script on my site. i followed part of a tutorial as i liked the friend adding part but didn't want to change the whole site. i used his code but obviously had to change some it to work on my site. the idea is you visit someone else's profile and you can click to either block or send a friend request.

i am not sure where the issue is. i cant see any thing wrong in the php but is is possible i am missing something there as i am no expert, i am even less of an expert with javascript/ajax so this leads me to believe i have broken something in that.

here are my codes.

    //Script on the profile.php page

    function friendToggle(type,user,elem){
var conf = confirm("Press OK to confirm the '"+type+"' action for user <?php echo $username; ?>.");
if(conf != true){
    return false;
}
_(elem).innerHTML = 'please wait ...';
var ajax = ajaxObj("POST", "friend_system.php");
ajax.onreadystatechange = function() {
    if(ajaxReturn(ajax) == true) {
        if(ajax.responseText == "friend_request_sent"){
            _(elem).innerHTML = 'OK Friend Request Sent';
        } else if(ajax.responseText == "unfriend_ok"){
            _(elem).innerHTML = '<button onclick="friendToggle(\'friend\',\'<?php echo $id; ?>\',\'friendBtn\')">Request As Friend</button>';
        } else {
            alert(ajax.responseText);
            _(elem).innerHTML = 'Try again later';
        }
    }
}
ajax.send("type="+type+"&id="+id);
    }


    //php script for the friend_system.php page

    <?php
    include_once("scripts/checkuserlog.php");

     ?>
   <?php
   if (isset($_POST['type']) && isset($_POST['id'])){
$id = preg_replace('#[^a-z0-9]#i', '', $_POST['id']);
$sql = "SELECT COUNT(id) FROM myMembers WHERE id='$id' AND activated='1' LIMIT 1";
$query = mysqli_query($db_conx, $sql);
$exist_count = mysqli_fetch_row($query);
if($exist_count[0] < 1){
    mysqli_close($db_conx);
    echo "$username does not exist.";
    exit();
}
if($_POST['type'] == "friend"){
    $sql = "SELECT COUNT(id) FROM blockedusers WHERE blocker='$id' AND blockee='$logOptions_id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $blockcount1 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM blockedusers WHERE blocker='$logOptions_id' AND blockee='$id' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $blockcount2 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$logOptions_id' AND user2='$id' AND accepted='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count1 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$id' AND user2='$logOptions_id' AND accepted='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count2 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$logOptions_id' AND user2='$id' AND accepted='0' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count3 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$id' AND user2='$logOptions_id' AND accepted='0' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count4 = mysqli_fetch_row($query);
    if($blockcount1[0] > 0){
        mysqli_close($db_conx);
        echo "$user has you blocked, we cannot proceed.";
        exit();
    } else if($blockcount2[0] > 0){
        mysqli_close($db_conx);
        echo "You must first unblock $user in order to friend with them.";
        exit();
    } else if ($row_count1[0] > 0 || $row_count2[0] > 0) {
        mysqli_close($db_conx);
        echo "You are already friends with $user.";
        exit();
    } else if ($row_count3[0] > 0) {
        mysqli_close($db_conx);
        echo "You have a pending friend request already sent to $user.";
        exit();
    } else if ($row_count4[0] > 0) {
        mysqli_close($db_conx);
        echo "$user has requested to friend with you first. Check your friend requests.";
        exit();
    } else {
        $sql = "INSERT INTO friends(user1, user2, datemade) VALUES('$logOptions_id','$id',now())";
        $query = mysqli_query($db_conx, $sql);
        mysqli_close($db_conx);
        echo "friend_request_sent";
        exit();
    }
} else if($_POST['type'] == "unfriend"){
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$logOptions_id' AND user2='$id' AND accepted='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count1 = mysqli_fetch_row($query);
    $sql = "SELECT COUNT(id) FROM friends WHERE user1='$id' AND user2='$logOptions_id' AND accepted='1' LIMIT 1";
    $query = mysqli_query($db_conx, $sql);
    $row_count2 = mysqli_fetch_row($query);
    if ($row_count1[0] > 0) {
        $sql = "DELETE FROM friends WHERE user1='$logOptions_id' AND user2='$id' AND accepted='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        mysqli_close($db_conx);
        echo "unfriend_ok";
        exit();
    } else if ($row_count2[0] > 0) {
        $sql = "DELETE FROM friends WHERE user1='$id' AND user2='$logOptions_id' AND accepted='1' LIMIT 1";
        $query = mysqli_query($db_conx, $sql);
        mysqli_close($db_conx);
        echo "unfriend_ok";
        exit();
    } else {
        mysqli_close($db_conx);
        echo "No friendship could be found between your account and $user, therefore we cannot unfriend you.";
        exit();
    }
}
   }
   ?>

i have been looking at it now for a couple of days and am starting to not see the wood for the trees.

When i click on the request as fiend button, i get the dialog box fine, click ok and then it replaces the button with "please wait..." but that is where it stops. i have checked and nothing is being added to the database niether.

any help you could offer would be much apreciated.

thanks

dyer926
  • 47
  • 1
  • 10
  • Are there any JavaScript errors in the console? Does the AJAX script run directly in the browser? – Blazemonger Sep 12 '13 at 16:22
  • You're not using any jQuery that I can see.... – Blazemonger Sep 12 '13 at 16:23
  • run the php file on its own from a url and see what it returns. if it returns the expected output then you know its the javascript – nathan hayfield Sep 12 '13 at 16:27
  • You need some background in using jQuery/AJAX. Take a look at [this SO post](http://stackoverflow.com/questions/18724011/post-not-posting-data/18724684#18724684) and experiment with the simple examples at bottom. – cssyphus Sep 12 '13 at 16:42

1 Answers1

0

I have provided an example of using jQuery to do this simply.

Here is what your button and response box would look like.

    <div id="responsemessage<?php ///YOU USER ID FROM PHP// ?>" style="padding:2px; display:none;"></div>

<input name="" type="button" value="Friend Me" onClick="friendToggle('friend','<?php ///YOU USER ID FROM PHP// ?>')"/>
<input name="" type="button" value="Block Me" onClick="friendToggle('block','<?php ///YOU USER ID FROM PHP// ?>')"/>

This is what your jQuery function would look like. You will need to include the jQuery lib in your header.

<script src="http://code.jquery.com/jquery-latest.min.js" type="text/javascript"></script>


<script>function friendToggle(type,user){


///This is the ajax request via jQuery///
 $.ajax({
  url: 'friend_system.php?action='+type+'&user='+user,
  success: function(data) {
  ///This is where the response from you php is handled. Sky's the limit//
    if(data == 'good'){
        $("#responsemessage"+user).html('You now have a friend.');
    }else{
    $("#responsemessage"+user).html(data);
    }

  }});



}</script>

</head>

And here is the php to process the requests this would be in your friend_system.php

<?php
include('YOUR CONNECTION DETAILS FILE');
$act = $_REQUEST['action'];

if($act == 'friend'){

$a = mysql_query("SELECT * FROM friends WHERE user1 = '".$_REQUEST['user']."'");

if(mysql_num_rows($a) > 0){
echo 'You are already friends.';
}else{
mysql_query("INSERT INTO friends SET user1 = '".$_REQUEST['user']."', user2 = '', datemade = '".date('d-m-Y H:i')."'");
echo 'good';
}

}

if($act == 'block'){

mysql_query("INSERT INTO blockedusers  SET blocker='YOUR ID HERE, HOPE ITS PASSED VIA SESSION' AND blockee='".$_REQUEST['user']."'");

echo 'You have blocked this user.';

}


?>

I hope this helps you... Also be sure to check out http://jquery.com/