javascript - Change a variable value on ajax success according to server response vai php

152
<table >
   <thead>
    <tr>
     <th>ID</th>
     <th>organisation Name</th>
     <th>User name</th>
     <th>Email</th>
     <th>Contact No</th>
     <th>IP</th>
     <th>Date</th>
     <th>Status</th>
    </tr>
   </thead>
   <tbody>
     <tr > <td>3</td>
    <td>Harshit </td>
    <td>AtulSaini</td>
    <td>[email protected]</td>
    <td>786048</td>
    <td>::1</td>
    <td>14/03/2015</td><td><button id="status"><span >Active</span></button></td></tr><tr > <td>4</td>
    <td>Meghaa.co.edu</td>
    <td>megha</td>
    <td>[email protected]</td>
        <td>786048</td>
        <td>::1</td>
        <td>14/03/2015</td><td><button id="status"><span class="label label-success">Active</span></button></td></tr>            

      </tbody>
    </table>

Hello to all I want last if condition to execute on the server response which alters the res variable The problem is when i put the whole if block of code in ajax success then variable "this" value get changed which disables the code to change the tr and if i keep it outside as i have done here then the variable res gets destroyed as it is in a success function

<script type="text/javascript">
$(document).ready(function() {
$(".label").click(function() {
    var idx = $(this).parents("tr").find('td:first').html();
    var status = $(this).parents("tr").find('span').html();
    var cname=$(this).parents('tr').attr("class");
    alert(this);

    $.ajax({
        url : "scripts/update.php",
        type : 'GET',
        data : {
            "status" : status,
            "id" : idx
        },
        error : function(xhr, status, error) {
            alert(error);
        },
        success : function(entry) {
            var res = entry;    

        }
    });
  alert(status);
    if (status == 'Active') {
                $(this).parents("tr").removeClass(cname);
                $(this).parents("tr").addClass("warning");
                $(this).removeClass("label-success");
                $(this).addClass("label-warning");
                $(this).text("Deactive");
                alert("Account Deacivated");
            } else {
                $(this).parents('tr').removeClass(cname);
                $(this).parents('tr').addClass("success");
                $(this).removeClass("label-warning");
                 $(this).addClass("label-success");
                $(this).text("Active");
                alert("Account Acivated");

         }
   });
   });   
   </script>

Thanx in advance for your help...

736

Answer

Solution:

I'm not exactly sure what you are trying to achieve (e.g. what kind of data is stored inres /entry). But maybe the following will help you:

<script type="text/javascript">
$(document).ready(function() {
  $(".label").click(function() {
     // store reference for later use (and to avoid creating the same object multiple times)
    var item   = $(this);

    // use the previously stored jQuery item instead of recreating it every time with $(...)
    var idx    = item.parents("tr").find('td:first').html();
    var status = item.parents("tr").find('span').html();

    // ...

    $.ajax({
      url: "scripts/update.php",
      type: 'GET',
      data: {
        "status": status,
        "id": idx
      },
      error: function(xhr, status, error) {
        alert(error);
      },
      success: function(entry) {
        // `item` will still reference to the label you clicked on
      }
    });

  });
});   
</script>
102

Answer

Solution:

Try this out :

...
var $this = $(this);
$.ajax({
        url : "scripts/update.php",
        type : 'GET',
        data : {
            "status" : status,
            "id" : idx
        },
        error : function(xhr, status, error) {
            alert(error);
        },
        success : function(entry) {
            changeStatuts(entry, $this);

        }
    });

    var changeStatuts = function(status, ele){

           if (status == 'Active') {
                ele.parents("tr").removeClass(cname);
                ele.parents("tr").addClass("warning");
                ele.removeClass("label-success");
                ele.addClass("label-warning");
                ele.text("Deactive");
                alert("Account Deacivated");
            } else {
                ele.parents('tr').removeClass(cname);
                ele.parents('tr').addClass("success");
                ele.removeClass("label-warning");
                ele.addClass("label-success");
                ele.text("Active");
                alert("Account Acivated");

         }
    };
528

Answer

Solution:

Okay, after reading your comment, maybe something like the following could solve your problem. BUT: Your PHP code seems to be vulnerable against a sql injection

<script type="text/javascript">
$(document).ready(function() {
  $(".label").click(function() {
    // store reference for later use (and to avoid creating the same object multiple times)
    var item   = $(this);

    // use the previously stored jQuery item instead of recreating it every time with $(...)
    var parentRow = item.parents("tr");
    var idx       = parentRow.find('td:first').html();
    var status    = parentRow.find('span').html();

    $.ajax({
      url: "scripts/update.php",
      type: 'GET',
      data: {
        "status": status,
        "id": idx
      },
      error: function(xhr, status, error) {
        alert(error);
      },
      success: function(status) {
        if (status == 'Active') {
          parentRow.removeClass(cname);
          parentRow.addClass("warning");
          item.removeClass("label-success").addClass("label-warning").text("Deactive");
          alert("Account Deacivated");
        } else {
          parentRow.removeClass(cname);
          parentRow.addClass("success");
          item.removeClass("label-warning").addClass("label-success").text("Active");
          alert("Account Acivated");
        }
      }
    });
  });
});   
</script>

People are also looking for solutions to the problem: php - Oracle OCI parallel connection to an encrypted and non-encrypted Oracle instance

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.