0

I want to do a MySQL,PHP,jQuery AJAX and JSON based friend request system like in Facebook. I have prepared the JSON results but I don't know how to do the jQuery part can anyone please help me here is the JSON results

this one when u have error

{ "status":"error", "message":"Invalid Attempt" }

if you are already friends

{
"status":"error",
"message":"Already Friends"
}

and for success

{
"status":"success",
"message":"Request sent"
}

after success message I want to hide the div HTML structure is

<div class="comment" id="uid-2">
    <a class="avatar" href="http://mysitecom/u/remya">
      <img src="http://mysitecom/uploads/avatars/remya_XysseWQ.jpg">
    </a>
    <div class="content">
      <a class="author" href="http://mysitecom/u/remya">Remya Prakash</a>
      <div class="text">
      <a class="author" href="javascript:;" id="2">Send Request</a>
      </div>
    </div>
  </div>
Irfan DANISH
  • 8,349
  • 12
  • 42
  • 67
Remya Prakash
  • 97
  • 1
  • 2
  • 7

1 Answers1

1

Return the status as http header, 201 for success, 409 for a friend that already exists and 400 for invalid attempt. Then, the response doesn't even need to be a json object, but plain text that you can show to the user.

 <div class="text">
      <a class="author" id="2" href="javascript:void(0)">Send Request</a>
      <div id="result"></div> 
  </div>

...

<script>

$('.author').click(function(){
  $.post('ajax-friend-request-url',{ 'id': this.id })
     .done(function(){
           $(this).hide();
           $('#result').css('background-color', 'green');                 
         })
     .fail(function(){
           $('#result').css('background-color', 'red');                
         })
     .always(function(data){
           $('#result').text(data);
         });
    });
</script>

http_response_code ([ int $response_code ] ) is the php function that can be used for setting the status header

martti
  • 1,758
  • 1
  • 12
  • 10