samedi 21 mars 2015

How to update more tables using ajax post with dynamic accept friends buttons?


i have a php file to accepting friends request.



<?php


session_start();


include 'db.php';


error_reporting(0);


$username= $_SESSION['username'];


$i=1;


$sql = "SELECT * FROM friends WHERE user2='$username' AND accepted='0' ORDER BY datemade DESC";


$user_query = mysqli_query($db, $sql);


$numrows = mysqli_num_rows($user_query);


if($numrows < 1){



echo "<center><p style='padding-left:0px;'>no more friend requests!<p></center>";


}


while ($row = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) {



$user1 = $row["user1"];

$logged_user= $row["user2"];

$i++;

?>

<div class="row" id="child" style="background-color: #c2c2c2;">

<div class="col-md-12">

<div class="col-md-4" style="margin-top:20px;padding-top: 0px;padding-right:0px;">

<img src="user/<?php echo $user1; ?>/profile_pictures/<?php echo $user1; ?>.jpg" width="25px" height="25px"/>

</div>

<div class="col-md-8" style="padding-left:0px; margin-left: -10px;">

<b><?php echo $user1 ?></b><br />

<div id="accept_div">

<input id="rcv_user" type="hidden" name="rcv_user" value="<?php echo $logged_user; ?>"/>

<input id="snd_usr" type="hidden" name="snd_usr" value="<?php echo $user1; ?>"/>
<input id="response" type="hidden" name="response" value="accept"/>



<span id="accepted" class="" style="display:none">Accepted</span>

<span id="or">or</span> <a id="ignore" href="#"><button class="btn-xs btn-danger">Ignore</button></a>


and script:



<script type="text/javascript">


$(document).ready(function () {


$('#submit').each(function() {



$(this).click(function() {

var ths = $(this);


var I = ths.attr("id");


var rcv_user = $(ths).siblings("#rcv_user").val();


var snd_usr = $(ths).siblings("#snd_usr").val();


var response = $(ths).siblings("#response").val();


$.post("respond_friend.php", {rcv_user:rcv_user, snd_usr:snd_usr, response:response},


function(value){


$('#child').css('background-color', 'white');


$("#submit").hide();


$("#ignore").hide();


$("#or").hide();



$("#accepted").show();

});


});


});


});


the problem here is if the user gets more than one friend request then he can only accept the first frnd request and the other accept buttons dont works. after refreshing the page only the next accept button works. it takes one refresh of page to accept one friend request. plz help me. thanx





Aucun commentaire:

Enregistrer un commentaire