-3
<script type="text/javascript">
var r = confirm("Press a button");
if (r == true) {
    <?php header('location:index.php');?>
}</script>

//Its redirect in index.php without conformation dialog and below code execute properly no any code contain simple navigation code..

<script type="text/javascript">
    var r = confirm("Press a button");
    if (r == true) {
       x = "You pressed OK!";
    }</script>

2 Answers2

0

You're mixing server-side and client-side functionality which makes it very confusing and it's not behaving the way you want it to.

Your header will be sent out by PHP from your server no matter what you do client-side. Use a JavaScript redirect instead to handle it client-side.

purpleninja
  • 376
  • 3
  • 11
0

hey You cann't use php in javascript like this. You can only use php in javascript , when you need a value. Because php will not execute after loading the page through javascript.

for example:

<script>
 var x="<?php echo $x; ?>";

</script>

but in your case , for redirection you can use

window.location="http://example.com/"