I am struggling with a solution to my problem. I have created the code and works fine in PHP but I now want to evolve this into a better working solution for the end user.
I have a set of links to confirm or deny an action. Once the users clicks “Confirm”/”Deny” I would like this to save value to db using AJAX and then make the li animate out with jQuery.
At the moment i have used php, which redirects to URL. Any help/guidance would be greatly appreciated.
Heres my code:
links
<?php
$friend_unconfirmed_from_them = mysql_query('
SELECT * from wallfriends
where mem_id_to ='.$USER->id.' AND confirm = 0
'); //unconfirmed friends to logged in user ($USER->id)
while ($friendrequest = mysql_fetch_array($friend_unconfirmed_from_them, MYSQL_ASSOC)) {
$senders_name = mysql_query('
SELECT firstname, lastname from mdl_user
where id = '.$friendrequest['sender'].'
'); //get unconfirmed friends name
while ($sender = mysql_fetch_array($senders_name, MYSQL_ASSOC)) {
?>
<li><?php echo $sender['firstname'].' '.$sender['lastname'];
}?>
<a href="confirmfriend.php?userid=<?php echo $friendrequest['sender']; ?>">Confirm Link</a>
<a href="removefriend.php?userid=<?php echo $friendrequest['sender']; ?>">Deny Link</a></li>
//echo each friend in li with confirm deny links
<?php } ?>
</ul>
confirmfriend.php
$userid = optional_param('userid', 0, PARAM_INT);
$connection = mysql_connect("localhost", "root", "on10n") or die ("Unable to connect!");
mysql_select_db("fe") or die ("Unable to select database!");
$query = "UPDATE wallfriends SET confirm = 1 WHERE mem_id_to = '$USER->id' AND mem_id_from = '$userid'";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (!$result){
echo "There were errors :<br>". mysql_error();
}
else{
redirect($siteUrl.'/profile.php?userid='.$userid.'');
die;
}
denyfriend.php
$userid = optional_param('userid', 0, PARAM_INT);
$connection = mysql_connect("localhost", "root", "on10n") or die ("Unable to connect!");
mysql_select_db("fe") or die ("Unable to select database!");
$query = "DELETE FROM wallfriends
WHERE (mem_id_from = '$USER->id' OR mem_id_from = '$userid')
AND (mem_id_to = '$USER->id' OR mem_id_to = '$userid')
";
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
if (!$result){
echo "There were errors :<br>". mysql_error();
}
else{
redirect($siteUrl.'/profile.php?userid='.$userid.'');
die;
}
youre not showing any javascript and the anchor will throw the page onto confirmfriend.php/removefriend.php.
Also you have multiple nested anchor tags (remember to close with
</a>)To achieve desired result, i’d do so:
Followed by a script which hooks onclick to anchors, sending an AJAX request.