js script inside php

Asked

Viewed 288 times

-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 first code I’m trying to show a confirm on screen to ask if really the user will want to delete that news.

In the 2nd code there is a link with a onclick for the Javascript function.

The problem is this by clicking on the delete link it just doesn’t show the confirmation window or anything, it’s like there’s no link at all.

  • 3

    Add the code in text, can not simulate the problem in a code of an image, easy to have the same in text.

  • Quiet ! Thank you for the tip

1 answer

1


In onClick, you need to pass the function call and not the reference to it. That is, need to have opening and closing parentheses after the function name "confirmation".

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

  • Didn’t work, mouse cursor changed to default and tbm n works

  • I made an example working. Please check in your browser element inspecter if your script has actually been inserted into the page

Browser other questions tagged

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