Abdul Waheed Abdul Waheed - 14 days ago 8
jQuery Question

How can i refresh notification count and remove parent when it is deleted

when delete trough ajax call how can remove that content from the view.?

this my ajax call and i am deleting a job from there when it deletes.


  1. that parent has to remove and

  2. the job notification count has to refresh(MY JOBS (3) it should change as 2 because there are only 2 jobs are there now in db when i logout and login it coming as 2.)



image of deleted job not parent is not deleted.

Here is my ajax controller code:

elseif(isset($_POST['res'])&& ($_POST['res'] =='no'))
{
$jobId=$_POST['jobId'];
$updateStatus=$conn->query("UPDATE r_job_invitations SET inv_res='2' WHERE id_job='".$jobId."' ");
if ($updateStatus) {
$response['message'] = "<strong>Success!</strong> Job Rejected.";
$response['success'] = true;
} else {
$response['message'] = "<strong>Warning!</strong> There is an error in Job Rejection please try again";
$response['success'] = false;
}
echo json_encode($response);
exit;
}


My ajax call and sending request:

<a href="javascript:;" class="btn btn-default" onclick="jobResponse('no',<?php echo $myjobs['id_job'];?>)">Reject And Delete</a>

<script type="text/javascript">
function jobResponse(res,jobId){
var frm_data = { res : res,
jobId : jobId
}
$.ajax({
method: "POST",
url: 'inv-controller.php',
data: frm_data,
dataType: "json",
success: function (response) {
if (response["success"] == true)
{
$("#success-message").show();
$("#success-message").html(response["message"]);
} else {
$("#warning-message").show();
$("#warning-message").html(response["message"]);
}
},
error: function (request, status, error) {
$("#warning-message").show();
$("#warning-message").html("OOPS! Something Went Wrong Please Try After Sometime!");
}
});
}
</script>


And finally this jobs count session is coming from login controller

while logging in i am stroing that jobs count in a session like this:

$Query = $conn->query("SELECT count(*) as notificationCount FROM r_job_invitations where email='".$_SESSION['user_email']."' and inv_res=0") or die(mysqli_error());
$notification = mysqli_fetch_assoc($Query);
$_SESSION['notificationCount'] = $notification['notificationCount'];
echo BASE_URL."my-profile.php";


I want to update this session count. how can i do that.?

Answer

on your ajax controller page:

if ($updateStatus) {
    $response['message'] = "<strong>Success!</strong> Job Rejected.";
    $response['success'] = true;
    $response['newCount'] = $_SESSION['notificationCount']+1;
    $_SESSION['notificationCount']++;
......

and then in your jquery

if (response["success"] == true)
            {  
                $("#success-message").show();
                $("#success-message").html(response["message"]);
                $('selector_that_has_your_old_count').html(response["newCount"]);