admin管理员组

文章数量:1356285

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 } ?>

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 } ?>
Share Improve this question asked Mar 25, 2014 at 20:34 B.MatthieuB.Matthieu 1151 silver badge10 bronze badges 6
  • console.log(typeof(r), r); – Adrian Preuss Commented Mar 25, 2014 at 20:36
  • I can't reproduce. – Daedalus Commented Mar 25, 2014 at 20:37
  • you can try if(r) instead if(r == true) – Fisherman Commented Mar 25, 2014 at 20:37
  • Add <?php header('Content-Type: text/plain'); ?> at the very top and then refresh your browser, take a look and read the code as the browser would "read" to execute it ... . – hakre Commented Mar 25, 2014 at 20:42
  • 1 The way it works is like this: browser makes request -> server receives request -> server executes PHP -> server sends response/HTML -> browser receives response/HTML -> browser parses HTML / executes JS – Felix Kling Commented Mar 25, 2014 at 20:50
 |  Show 1 more ment

3 Answers 3

Reset to default 15

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.

PHP runs before JavaScript.

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

is already submitted when browser load JavaScript.

<?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.

本文标签: phpjavascript confirm() is always truewhats wrongStack Overflow