JavaScript command does not work inside Buttom

2

Good morning! I have a problem and I can not resolve the issue. I need a buttom to appear or not according to the PHP condition. Making the buttom appear I'm getting, the point is that inside it has a javascript call that does not work inside PHP. I do not know what I'm doing wrong.

Here are the codes:

<header>
<script type="text/javascript">
function exibe(id) {
    if(document.getElementById(id).style.display=="none") {
        document.getElementById(id).style.display = "inline";
    }
    else {
        document.getElementById(id).style.display = "none";
    }
}
</script>
</header>

<body>
<?php  
    if (get('data_do_descredenciamento')!= ''){
        echo '<button type="button" class="btn" href="#" onclick="javascript: exibe('conteudo');">SUBSTITUTO</button>';
    }
?>
</body>

You're giving content error: onclick="javascript: exibe('conteudo')

Could someone help me, please?

    
asked by anonymous 01.08.2016 / 14:14

2 answers

2

Your problem is because of the quotes. Here are some alternatives to solve your problem:

1 ° Concatenate quotes.

echo "<button type='button' class='btn' onclick="."exibe('conteudo');".">SUBSTITUTO</button>";

2nd Escape single quotes

echo '<button type="button" class="btn" href="#" onclick="javascript: exibe(\'conteudo\');">SUBSTITUTO</button>';

3rd Variable with onclick

$button_onclick = "onclick="."exibe('conteudo');"."";
echo "<button type='button' class='btn' href='#' $button_onclick>SUBSTITUTO</button>";

I hope I have helped.

    
01.08.2016 / 14:38
1

Replace this:

onclick="javascript: exibe('conteudo');"

So:

onclick="javascript: exibe(\'conteudo\');"
    
01.08.2016 / 15:18