B.Matthieu
B.Matthieu

Reputation: 115

javascript confirm() is always true...whats wrong?

When I press okay, everything works fine so far. But when I press cancel, it does the exact thing that if I pressed okay...what's wrong with my code? :/

<?php if(isset($_POST['supprimer'])) { ?>   
    <script>var r = confirm('Etes-vous sur de vouloir supprimer?');
    if(r == true) { <?php $object->supprimer($_POST['rowID']); ?> 
    }</script> 
    <?php } ?>

Upvotes: 1

Views: 340

Answers (3)

Mohini
Mohini

Reputation: 407

<?php
$supreme = 'yes';
if(isset($supreme)) { ?>
<script>
var r = confirm('Etes-vous sur de vouloir supprimer?');
alert(r);
if(r == true) { 
 alert('pressed Yes');
}
else
{alert('pressed Cancel');} 
</script> 
<?php } ?>

Try the above code - it will reflect the value chosen in the confirm popup.

Upvotes: 2

Kristiyan
Kristiyan

Reputation: 1663

PHP runs before JavaScript.

$object->supprimer($_POST['rowID']) 

is already submitted when browser load JavaScript.

Upvotes: 3

Anonymous
Anonymous

Reputation: 12017

Your inner PHP script executes regardless of the JavaScript conditional since PHP runs before the page is rendered, thus, before JavaScript. You would need to use ajax or a page reload in order to have PHP that runs on a conditional JavaScript statement.

Upvotes: 15

Related Questions