Change student status from active to inactive

Asked

Viewed 567 times

1

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

I know how to do php queries until I display the status, but I don’t know how to create the link with the word and call javascript and update again. What is the best method ? Some example code ?

  • 1

    Use a mysqli_query with UPDATE table SET status=status WHERE id in ('id_do_aluno')

  • 1

    That’s right, buddy, but no refresh ? take a look at me if, the whole part of php is quiet, the problem is changing the name from active to disabled with javascript or some other way.

  • 1

    and if it were a .php external?

  • 1

    Yes, I thought I could without a javascript that calls an external php with POST and then query new status and go back to page and update only the active word.

  • 1

    The problem is that I don’t know how to do this with javascript.

1 answer

1


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

Code Example

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.php action should be ready to execute the actions when accessed let’s assume that the makeRequest() function was called as follows : makeRequest('inactive'), php will have to look like this one:

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

Knob

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

2° Function alertContents() Soon 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>');
    }
    }

Inside a script tag

  1. Knob:

In php put the button code inside div with the id 'button' before the Jquery library import is:

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

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

  1. Click the button

In the script tag in which the alertContents() function was declared before closing the tag paste the following code:

$("#ativo").click(function(){
   makeRequest('ativo');
});
 $("#inativo").click(function(){
   makeRequest('inativo');
});
  • 1

    Hello, I have to import the ajax library right ? am receiving the following error: " Referenceerror: acao is not defined at Htmlbuttonelement.onclick" my button: <button value="" onclick="acao()"> <?php echo $active;? > </button>

  • Solved ? If yes mark as the correct answer on the 'v' icon on the left side of the question

  • I test as soon as I get home friend plus the night, thanks in advance.

  • Thank you for

Browser other questions tagged

You are not signed in. Login or sign up in order to post.