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
3 Answers
Reset to default 15Your 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
版权声明:本文标题:php - javascript confirm() is always true...whats wrong? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744028172a2578419.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论