2
I have a code where the customer clicking the button, it will activate or not a user, and of course, change the status in the database. So far everything ok. However, the code has two buttons, On and Off, and initially the Off button is hidden by default. See below:
$('button#btn-cancelar').hide();
So in PHP I did this way:
if($jmVagas->StatusVagas == 'A'){
$botao = "$('button#btn-cancelar').hide();";
}if($jmVagas->StatusVagas == 'N'){
$botao = "$('button#btn-salvar').hide();";
}
But when you click the button, it changes the status in the bank correctly, but the view appears the same in both. I will post the code for you:
Jquery:
<script type="text/javascript">
$(document).ready(function(){
// $('button#btn-cancelar').hide();
<?php
echo $visualizarVagas[1];
?>
$('button#btn-salvar').click(function(){
$('button#btn-salvar').hide();
$('button#btn-cancelar').show();
var valor = $(this).attr('value');
$('button#btn-cancelar').text("Desativado").attr({
title:"Desativado"
});
jQuery.ajax({
url : "alterar.php?v=N&k="+valor,
dataType : 'json',
async : false,
success : function(msg) {
}
});
});
$('button#btn-cancelar').click(function(){
$('button#btn-salvar').show();
$('button#btn-cancelar').hide();
var valor = $(this).attr('value');
$('button#btn-salvar').text("Ativado").attr({
title:"Ativado"
});
jQuery.ajax({
url : "alterar.php?v=A&k="+valor,
dataType : 'json',
async : false,
success : function(msg) {
}
});
});
});
</script>
PHP:
while(...){
if($jmVagas->StatusVagas == 'A'){
$botao = "$('button#btn-cancelar').hide();";
}if($jmVagas->StatusVagas == 'N'){
$botao = "$('button#btn-salvar').hide();";
}
$listar .= "<button class=\"btn btn-xs btn-primary\" id=\"btn-salvar\" value=\"".$jmVagas->IdVagas."\" title=\"Ativado\">Ativado</button>";
$listar .= "<button class=\"btn btn-xs btn-danger\" id=\"btn-cancelar\" value=\"".$jmVagas->IdVagas."\" title=\"Desativado\">Desativado</button>";
}
Why you do not work the IDE in ajax Success instead of putting the jquery command in a php variable ?
– Gabriel Rodrigues
Hello Gabriel. Forgive me the ignorance, because I do not know very well ajax/jquery. How would do this exactly?
– user24136