Change student status from active to inactive

1

I have a bank where saved status = 1 for active, and 2 for inactive. Is there any way I can deploy to the page the writing or a button (or link in the word) "active" and when clicking, change to "inactive" and change the server from 1 to 0 without refresh on the page.

I know how to query php until the status is displayed, but I do not know how to create the link with the word and call javascript and refresh again. What is the best method? Any sample code?

    
asked by anonymous 03.09.2017 / 02:22

1 answer

1

You can use AJAX, everything should be done by another page and the one where the user is only going to send the commands (make the requisitions)

Code Sample

var httpRequest = new XMLHttpRequest();
function makeRequest(acao) { //chame a função dizendo qual será a ação 
    httpRequest.open('GET', 'action.php?action='+acao); //Diga o método e a URL 
    httpRequest.send();
    httpRequest.onreadystatechange=function(){ //Quando o retorno estiver pronto
        if (httpRequest.readyState === 4) {//Processo concluído
            alertContents(); //Função que dirá que a função foi completada
        }
    }
}

Action.php

The action.php should be ready to perform the actions when it is accessed let's assume that the makeRequest () function was called as follows: makeRequest ('inactive'), php will have it with a code similar to this:

switch ($_GET['action']){
   case 'inativo':
      //o que o php deverá fazer
   ;
}

Button

1st Import the JQuery library <script src="//code.jquery.com/jquery-1.12.0.min.js"></script> place before the closing of the body tag.

2nd function alertContents () Immediately after importing the JQuery library use the following code:

function alertContents(){
   if($('#button').html() == '<button id="ativo">Deixar Ativo</button>'){
      $('#button').html('<button id="inativo">Deixar Inativo</button>')
        }
   else{
      $('#button').html('<button id="ativo">Deixar Ativo</button>');
    }
    }

Within a tag script

  • Button:
  • In php put the button code inside div with the 'button' id before the import of the JQuery library being:

    Inactive <button id="inativo">Deixar Inativo</button>

    For Active <button id="ativo">Deixar Ativo</button>

  • Click the
  • In the script tag where the alertContents () function was declared before tag closing, paste the following code:

    $("#ativo").click(function(){
       makeRequest('ativo');
    });
     $("#inativo").click(function(){
       makeRequest('inativo');
    });
    
        
    03.09.2017 / 03:06