Rename the button

Asked

Viewed 611 times

-2

In my code I have a save button for each option that I made and now I need, when saving, the name of the button to change.
How should I proceed?

código:

    // aqui eu verifico o nome do botão
    if(isset($dados) && ($dados['ecocodigocolecao2']!= '' || $dados['ecocodigocolecao1'] != '')){
    $nomebotao == 'Alterar';
    }else{
    $nomebotao == 'Salvar';
    }
<td>
    <?php echo $db->monta_combo("ecocodigocolecao1", $adesao->listarLivros($disciplina['codigocomponente'],1,""), $podeeditar,  "Selecione...", "", "", "", "500", "N", $padrao_id."_1", "", isset($dados)?trim($dados['ecocodigocolecao1']):'""',"","","onchange=  \"buscarsegundaop(this.value,{$disciplina['codigocomponente']},{$disciplina['codigoetapa']})\"");?>
</td>
<td>
    <?php echo $db->monta_combo("ecocodigocolecao2", $dados2, isset($dados)&& trim($dados['ecocodigocolecao2'])==""?"N":"{$podeeditar}", "Selecione...", "", "", "", "500", "N", $padrao_id."_2", "", isset($dados)?trim($dados['ecocodigocolecao2']):'""');?>
</td>
<td>
    <input type="button" id="salvar" name="salvar" 
    <?php echo $podeeditar=="N"?'disabled="disabled"':''?> onclick="salvarcolecao(
<?php echo $disciplina['codigoetapa']?>,
<?php echo $disciplina['comid']?>,
<?php echo $row['adeid']?>,$(this));" value="$nomebotao"/>
</td>
  • I think the question will be for javascript/jquery and not php.

  • What does save collection function do? is that where you should change the name

  • @Tiagogomes makes an ajax request, and returns the success or failure message.

1 answer

2

You can add a new parameter and pass the field using the this:

HTML:

<!-- código omitido -->
<td>
    <input type="button" id="salvar" name="salvar" 
    <?php echo $podeeditar=="N"?'disabled="disabled"':''?> onclick="salvarcolecao(
    <?php echo $disciplina['codigoetapa']?>,
    <?php echo $disciplina['comid']?>,
    <?php echo $row['adeid']?>, this);" value="<?php echo $condicao ? 'salvar' : 'alterar' ?>"/>
</td>

JS:

function salvarcolecao(codigoetapa,comid,adeid, campo){
    $("#ecocod_"+codigoetapa+"_"+comid+"_2").attr('disabled',false);
    ecocodigocolecao1 = $("#ecocod_"+codigoetapa+"_"+comid+"_1").val();
    ecocodigocolecao2 = $("#ecocod_"+codigoetapa+"_"+comid+"_2").val();
    if (verificarRegras(ecocodigocolecao1,ecocodigocolecao2)){
        //alert(ecocodigocolecao1+"---"+ecocodigocolecao2+'=-===='+"#"+codigoetapa+"_"+comid+"_1");
        jQuery.ajax({
            type: "POST",
            url: "pddeinterativo2015.php?modulo=principal/escolha/principalEscolha&acao=A&aba=Escolha",
            data: "action=salvarEscolha&ecocodigocolecao1="+ecocodigocolecao1+'&ecocodigocolecao2='+ecocodigocolecao2+'&comid='+comid+'&adeid='+adeid,
            async: false,
            success: function(msg){
                alert(msg);

                // alterando o valor do campo
                campo.value = (campo.value == "salvar" ? "alterar" : value);
            }
        });
    }
}
  • You should pass this and change inside the function. If ajax fails? you should not change the button

  • @Tiagogomes can help me with an example?

  • I’ll change the post right away.

  • @Laerte , another situation is: doing this way when updating the page, the name of the button is saved again, and should be changed.. which happens, after I save, whenever reload the page my options already come loaded with the value saved, but the button remains with the name save.

  • How do you know it changed the data? Because when rendering with PHP you can put this condition in the input.

  • I updated, in case you give me the field you check to see if you’ve saved so I change the answer.

  • @Laerte I did the following: in my PHP I added the $placeholder variable instead of the value="save" input. and in onclick I added $(this) with parameter. in my js function I added btn as this parameter and the following line after ajax sucess: btn.val('Change');

  • Thanks! It worked out here.

  • Mark as answer to help others. ;)

Show 4 more comments

Browser other questions tagged

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