Javascript function concatenation in onclick inside php

Asked

Viewed 382 times

0

Please help me out

<?php
          if($dados_login['acesso'] >= 3){
            echo("<div class='col-md-2'>
                         <button type='button' onclick='window.open('icg_historico_excluidos.php?id=".$id."&perfil=1&nome_aps=".$dados_APS['nome_aps']."', '', 'height=690, width=1050, top=90, left=500, scrollbars=yes, resizable=no');'' class='btn btn-block btn-warning'>Histórico da Exclusão de ICG </button>
                    </div>");
          }
        ?>
  • 2

    Hello Jennifer, so that people can help you and better understand your problem I recommend you copy a larger part of the code with the critical points and use the code marking in the Question editor. I recommend this link.

2 answers

0

Hello Jennifer I do not know if my code will run, however I think so open your scope of possible ways to solve this kind of complications with strings in PHP;

<?php
if($dados_login['acesso'] >= 3):?>
    <div class='col-md-2'>
        <button type='button' onclick='window.open("icg_historico_excluidos.php?id=<?= $id ?>&perfil=1&nome_aps=<?=$dados_APS['nome_aps']?>,height=690, width=1050, top=90, left=500, scrollbars=yes, resizable=no");' class='btn btn-block btn-warning'>Histórico da Exclusão de ICG </button>
    </div>
    <?php endif ?>

0


Let’s see...

As a general rule, when inserting HTML elements through PHP, you should always:

1 - Use double escaped quotes for element attributes. To escape the quotes, simply put the backslash before it will be interpreted as a character of the string and not string delimiting. Example:

<?php
    echo "<div class=\"minha_classe\"></div>";

    // Resulta no HTML <div class="minha_classe"></div>
?>

2 - If you need to use single quotes for some internal string of the attribute - as in the case of a Javascript string of a function in some on attribute (onclick, onblur...) - there will be no conflict if you followed what is found in 1. Example:

<?php
    // Forma sem conflito:
    echo "<div onclick=\"alert('teste');\"></div>";

    // Resulta no HTML <div onclick="alert('teste');"></div>
?>


<?php
    // Forma com conflito:
    echo "<div onclick='alert('teste');'></div>";

    // Resulta no HTML <div onclick='alert('teste');'></div>
?>

By following these rules, you can easily concatenate strings in PHP to take advantage of the values of the variables without errors. There will be no conflict be done correctly.

Your code

I gave a "slap" in your code to better understand what was done. This way, it is easier to avoid affecting errors. Consequently, removed any possible error.

You can change or rearrange variables and their names in any way you like. There is no single way to organize. But it’s important to be organized!

<?php

    if( $dados_login["acesso"] >= 3 ){

        // Construção dos argumentos de window.open(...)
        $uarg = "id=" . $id . "&perfil=1&nome_aps=" . $dados_APS['nome_aps'];
        $url  = "icg_historico_excluidos.php?" . $uarg;

        $name = "";

        $spec = "height=690,width=1050,top=90,left=500,scrollbars=yes,resizable=no";

        // Argumentos completos de window.open(...)
        $fargs = "'" . $url . "', '" . $name . "', '" . $spec . "'";

        // HTML completo
        echo "<div class=\"col-md-2\">";

        echo "<button type=\"button\" onclick=\"window.open(" . $fargs . ");\" ";
        echo "class=\"btn btn-block btn-warning\">";

        echo "Histórico da Exclusão de ICG";

        echo "</button>";

        echo "</div>";

    }

?>

It is worth noting that if there are quotes in the variables used, it may cause some conflict. Then the recommendation is pay close attention and plan well the way it will join all quotes, considering all text sources and its possible content! ]

That is: make sure the content of $dados_APS['nome_aps'] because it can cause conflict. And happy hacking!

Browser other questions tagged

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