无论是ajax还是php都破了,但无法看到哪里

时间:2013-09-12 16:17:29

标签: javascript php jquery ajax

我的网站上有一些脚本有问题。 我按照教程的一部分,因为我喜欢添加部分的朋友,但不想改变整个网站。 我使用他的代码,但显然必须改变一些它在我的网站上工作。 想法是你访问别人的个人资料,你可以点击阻止或发送朋友请求。

我不确定问题出在哪里。我不能在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();
    }
}
   }
   ?>

我现在已经看了好几天了,我开始看不到树木了。

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

你能提供的任何帮助都会得到很多帮助。

谢谢

1 个答案:

答案 0 :(得分: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 lib。

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