警报框不能与PHP一起使用

时间:2016-03-29 23:41:57

标签: javascript php

<?php if ($data == "") { ?>
    <script type="text/javascript">
        alert("review your answer1");
        window.location.href = "index1.php";
    </script>
<?php } else { ?>
    <script type="text/javascript">
        alert("review your answer2");
        window.location.href = "index2.php";
    </script>
<?php } ?>

警告框未显示在上面的代码中,页面也没有重定向。 有什么问题?

2 个答案:

答案 0 :(得分:4)

反过来说:

<script>
  var data = <?php echo $data; ?>
  if ( data === "" ) {
    alert("review your answer1");
    window.location.href = "index1.php";
  } else {
    alert("review your answer2");
    window.location.href = "index2.php";
  }
</script>

答案 1 :(得分:0)

改进您的代码,可能需要进行一些调整:

<script>
   alert("review your answer<?echo $data;?>");
   window.location.href = "index<?echo $data;?>.php";
</script>