0

我在我的网站上遇到了一些脚本问题。我遵循了教程的一部分,因为我喜欢朋友添加的部分,但不想更改整个站点。我使用了他的代码,但显然必须更改一些代码才能在我的网站上运行。这个想法是您访问其他人的个人资料,然后您可以单击以阻止或发送好友请求。

我不确定问题出在哪里。我在 php 中看不到任何错误,但可能我在那里遗漏了一些东西,因为我不是专家,我更不是 javascript/ajax 专家,所以这让我相信我已经打破了一些东西。

这是我的代码。

    //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();
    }
}
   }
   ?>

我已经看了几天了,开始看不到树木的树林。

当我单击请求作为恶魔按钮时,我得到对话框很好,单击确定,然后它将按钮替换为“请稍候......”但这就是它停止的地方。我已经检查过,没有任何东西被添加到数据库中。

您可以提供的任何帮助将不胜感激。

谢谢

4

1 回答 1

0

我提供了一个使用 jQuery 简单地做到这一点的示例。

这是您的按钮和响应框的外观。

    <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// ?>')"/>

这就是你的 jQuery 函数的样子。您需要在标题中包含 jQuery 库。

<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>

这是处理请求的php,这将在您的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.';

}


?>

我希望这对您有所帮助...另外请务必查看http://jquery.com/

于 2013-09-12T16:56:45.717 回答