Problem with running SQL through FORM

Asked

Viewed 38 times

0

Php code:

if($_REQUEST['alterarStatus']){ $alterarStatus = trataaspas($_REQUEST['alterarStatus']);}

if($alterarStatus=="aprovado"){

$SQL = "update ps set StatusTransacao='Aprovado' where Referencia = '52'";  

}elseif($alterarStatus=="completo"){
}elseif($alterarStatus=="cancelado"){
}elseif($alterarStatus=="devolvido"){
}

JS Code:

function alterarStatusFunction(val){
if(val =="aprovado"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para APROVADO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="completo"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para COMPLETO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="cancelado"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para CANCELADO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
else if(val =="devolvido"){
if (confirm('Tem certeza que deseja alterar o status dessa transação para DEVOLVIDO?')) {
document.formtransacoes.submit();
} else {
return false;
}
}
}

HTML code:

<form name="formtransacoes" id="formtransacoes" action="" method="post">
<?
while($obj = mysqli_fetch_object($result)){
    $statustranssacao = $obj->StatusTransacao;
?>
    <tr>
    <td nowrap="nowrap" align="center">
    <?  if($obj->TransacaoID!=''){?>
    <select style="width:200px" onchange="alterarStatusFunction(this.options[this.selectedIndex].title);" name="alterarStatus">
        <option selected value="0">- Opções -</option>
        <option disabled="disabled">------------------------------------------</option>
        <? if($statustranssacao!='Aprovado' && $statustranssacao!='Completo'){?><option title="aprovado" value="<?=$obj->Referencia;?>">Marcar como APROVADO</option><? } ?>
        <? if($statustranssacao!='Completo' && $statustranssacao=='Aprovado'){?><option title="completo" value="<?=$obj->Referencia;?>">Marcar como COMPLETO</option><? } ?>
        <? if($statustranssacao!='Cancelado'){?><option title="cancelado" value="<?=$obj->Referencia;?>">Marcar como CANCELADO</option><? } ?>
        <? if($statustranssacao!='Devolvido'){?><option title="devolvido" value="<?=$obj->Referencia;?>">Marcar como DEVOLVIDO</option><? } ?>
    </select>
    <? }else{ ?>
    <span>Transação não iniciada!</span>
    <? } ?>
    </td>
    </tr>
<? } } ?>       
</form>

Guys, I can’t get this train to work. JS functions work normally, the problem is to execute the query according to the selected option. Someone has some hint?

  • It’s all one file or are they separate?

  • Everything in one file, I didn’t put all the code here pq is quite extensive, so I put the most important parts.

2 answers

0

The error is in your PHP code. Instead of you putting it on:

if($_REQUEST['alterarStatus']){ $alterarStatus = 
trataaspas($_REQUEST['alterarStatus']);}

if($alterarStatus=="aprovado"){

$SQL = "update ps set StatusTransacao='Aprovado' where Referencia = '52'";  

}elseif($alterarStatus=="completo"){
}elseif($alterarStatus=="cancelado"){
}elseif($alterarStatus=="devolvido"){
}

You must for this:

if($_REQUEST['alterarStatus']){ $alterarStatus = trataaspas($_REQUEST['alterarStatus']);}

    if($alterarStatus=="aprovado"){

        $SQL = "update ps set StatusTransacao='Aprovado' where Referencia = '52'";  

    }elseif($alterarStatus=="completo"){
        $SQL = "update ps set StatusTransacao='completo' where Referencia = '52'";
    }elseif($alterarStatus=="cancelado"){
        $SQL = "update ps set StatusTransacao='cancelado' where Referencia = '52'";
    }elseif($alterarStatus=="devolvido"){
        $SQL = "update ps set StatusTransacao='devolvido' where Referencia = '52'";
    }
  • Leandro, I know I was missing the query for the other options, I left only the first to test. The problem is that it does not work, it does not execute the query of any option.

  • Maybe you need to change the code, because each option has a different value, so I don’t know how to do in PHP.

0

It’s not working because when you send the form through Javascript, you automatically reset the variable, and then when you try to read it by php, the page has already reloaded. Do the following test, in the form, change the method to "get". These ifs:

if($alterarStatus=="aprovado"){
$SQL = "update ps set StatusTransacao='Aprovado' where Referencia = '52'";  
}elseif($alterarStatus=="completo"){
}elseif($alterarStatus=="cancelado"){
}elseif($alterarStatus=="devolvido"){
}

Change to:

if(isset($_GET['alterarStatus'])){ //Verifica se existe, pra nao ficar dando erro
    $status = $_GET['alterarStatus']; //aqui pegamos o valor selecionado por get
}

if($status=="aprovado"){
echo $SQL = "update ps set StatusTransacao='Aprovado' where Referencia = '52'";  

}elseif($status=="completo"){
}elseif($status=="cancelado"){
}elseif($status=="devolvido"){
}
  • Blz, I’ll test it and I’ll tell you what. A question, how could I get the value of the option? In case it would be replaced in Reference = '52', why each option has a different value.

  • The get takes just that, the value

  • I edited your answer, it would look like this in the case?

  • It will not work, as each option has a different value it will not be able to execute the correct query, which in the case is separated by approved, complete, canceled, returned. These status I put in the title attribute. How could I do?

  • In this case, in addition to the value, you want to pass an id, right? To do something like: "update table set situacao = 'complete' Where id = 52". Do you want to pass the situation and the id? If it is, yes! Just put the id/reference in the action, and then recover the same way I did with the options. It would look like this: <form name="formtransacoes" id="formtransacoes" action="<?echo '?'.$obj->Referencia;?>" method="post">

Browser other questions tagged

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