0

Is there away to alert this variable without using ajax? need guide, trying todo this in one page heres my code.

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

im trying to alert how many days left, does the user have before its credit balance expires, i think you cant combine php with jquery, but is there away to do this?

1
  • No again, the jQuery library is loaded after PHP. Although the alert will work after the PHP has finished executing. Commented May 7, 2014 at 10:13

2 Answers 2

2

You can call PHP in javascript, but not javascript in PHP.

Also you don't need to do an OR, you can just check if $totalactive is less than or equal to 10 using <=.

<?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
}
Sign up to request clarification or add additional context in comments.

1 Comment

Nicely done. (Side note: We have the same penguins ;))
0

Another way, which could look more flexible if you want to use that variable in functions or just keep your javascript code in your .js files, would be to get the value in a hidden element such as:

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

Then you would only need to retrieve it with jQuery or javascript on document ready:

$(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");

});

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.