js script within php

-1

1st Code :

$urlExclusao  = "index.php?".PARAMETER_NAME_ACTION."=delete&";
$urlExclusao .= PARAMETER_NAME_FILE."=noticia";
$urlExclusao .= "&id=".$row->noticiaid;

$test = $urlExclusao;
echo "
<script type='text/javascript'>
    function confirmation() {
    var answer = confirm('Deseja excluir essa Noticia ?');
    if (answer = true){
        window.location.href = ". $test ";
    }
   }
</script>";  

2nd Code :

<a onclick=\"confirmation()\"><img src=\"".DIR_ICONS."delete.png\" title=\"Excluir\" /></a>";

In the 1st code I'm trying to show a confirm on the screen to ask if the user really wants to delete this news.

In the 2nd code you have a link with a onclick for the JavaScript function.

The problem is the following when clicking the delete link it just does not show the confirmation window or anything, it's as if there was no link at all.

    
asked by anonymous 22.04.2016 / 18:15

1 answer

1

In onClick, you need to pass the function call, not the reference to it. That is, you need to open and close the parentheses after the name of the "confirmation" function.

function confirmation(){
  location.href = confirm("Deseja ir para o site do google") ? "http://www.google.com.br" : '';
}
<a href="javascript:void(0)" onClick="confirmation()"> Ir para o google </a>
    
22.04.2016 / 18:21