Ajax implementations are similar to the principle of the new wave feature.

64 4

I do this as a function, with no js code, to get the information to the database and refresh the page again, read the database, think it's good, know that the fun.

1 Answers

136 2

User. Php.

<?php if ($isFriend) {?>
 <a id="info_block_btn" class="info_btn_hover" href="a_relation.php?action=destory&id=<?php echo $userid?>">取消关注</a>
 <?php } else {?>
 <a id="info_follow_btn" class="info_btn" href="a_relation.php?action=create&id=<?php echo $userid?>">关注此人</a>
 <?php }?>

A_relation. Php.

<?php 
 include ('lib/twitese.php');
 $title ="修改好友关系";
 header("content-type:text/html; charset=utf-8");
 if (!isLogin()) header('location: login');
 if ( isset($_POST['action']) && isset($_POST['id']) ) {
 $t = getTwitter();
 if ($_POST['action'] == 'create') {
 $result = $t->followUser($_POST['id']);
 if ($result) echo '添加好友成功,请<a href="home">返回首页</a>或后退';
 else echo '添加好友出错,请返回重试';
 } else if ($_POST['action'] == 'destory') {
 $result = $t->destroyUser($_POST['id']);
 if ($result) echo ' 删除好友成功,请<a href="home">返回首页</a>或后退';
 else echo '删除好友出错,请返回重试';
 }
 } else {
 if ( isset($_GET['action']) && isset($_GET['id']) ) {
 $id = $_GET['id'];
 $action = $_GET['action'];
 $msg = $action == 'create'?"确定添加 $id 为好友?" :"确定删除好友 $id?";
 echo"
 <form action="a_relation.php" method="post">
 $msg
 <input type="hidden" name="id" value="$id"/>
 <input type="hidden" name="action" value="$action"/>
 <input type="submit" value="确定"/>
 <a href="home">取消</a>
 </form>
";
 } else {
 echo"非法请求";
 }
 }
?>

User. Js.

$("#info_follow_btn").live("click", function(e){
 e.preventDefault();
 var $this = $(this);
 var id = $("#info_name").text();
 if (confirm("你确定要关注" + id +"吗?")) {
 updateSentTip("正在关注" + id +"...");
 $.ajax({
 url:"ajax/relation.php",
 type:"POST",
 data:"action=create&id=" + id,
 success: function(msg) {
 if (msg.indexOf("success")> = 0) {
 updateSentTip("关注" + id +"成功。");
 $this.after('<a class="btn btn-red" id="info_block_btn" href="javascript:void(0)">取消关注</a>');
 $this.remove();
 } else {
 updateSentTip("关注" + id +"失败,请重试。(502)");
 }
 },
 error: function(msg) {
 updateSentTip("关注" + id +"失败,请重试。(403)");
 }
 });

Relation. Php.

<?php
 if(!isset($_SESSION)){
 session_start();
 }
 include ('../lib/twitese.php');
 $t = getTwitter();
 $result;
 if ( isset($_POST['action']) && isset($_POST['id']) ) {
 switch($_POST['action']){
 case 'create':
 $result = $t->followUser($_POST['id']);
 break;
 case 'destory':
 $result = $t->destroyUser($_POST['id']);
 break;
 case 'block':
 $result = $t->blockUser($_POST['id']);
 break;
 case 'unblock':
 $result = $t->unblockUser($_POST['id']);
 break;
 case 'report':
/* uncomment for test */
//$result = true;
 $result = $t->reportSpam($_POST['id']);
 break;
 }
 if ($result){
 refreshProfile($t);
 echo 'success';
 }else{
 echo 'error';
 }
 }

Reference
From fanfouapi, a fanfou style twitter client.

...