Jquery Alert PHP variable


Jquery Alert PHP variable

是否可以在不使用ajax的情况下提醒该变量?需要指导,尝试在一页中做到这一点,这里是我的代码。

<?php
if($totalActive == 10 OR $totalActive < 10){
echo "<input id='activo' type='hidden' value='$totalActive'>";
?>
    <script type="text/javascript">
    var tom = $("#activo").val();
    alert("Your Credit Balance will expires in  "+ tom +"days 'n Please buy more credits to extend the expire date");
    window.location.href = "Outbox.php";
    </script>
<?php
}//continue php scripts

我试图提醒多少天剩下,用户有之前的信用余额到期,我认为你不能结合PHP与jquery,但有没有办法做到这一点?

可以在javascript中调用PHP,但不能在PHP中调用javascript。

你也不需要做一个OR,你可以只用<=检查$totalactive是否小于或等于10

<?php
  if($totalActive <= 10){
?>
    <script type="text/javascript">
    alert("Your Credit Balance will expires in <?php echo $totalActive; ?> days 'n Please buy more credits to extend the expire date");
    window.location.href = "Outbox.php";
    </script>
<?php
}

如果您想在函数中使用该变量,或者只是将javascript代码保存在.js文件中,那么另一种看起来更灵活的方法是在隐藏元素中获取值,例如:

<input id="expireDays" type="hidden" value="<?php echo $totalActive; ?>" />

然后你只需要用jQuery或javascript在文档准备好时检索它:

$(document).ready(function() {
   var expiredDays = $('#expireDays').val();
   alert("Your Credit Balance will expires in  "+ expiredDays +"days 'n Please buy more credits to extend the expire date");
});