0

I'm trying to add an HTML next to an existing element in my page. This is what I've tried::

HTML/PHP

<?php 
if ( $duedatereservation < $today ) {
//echo "Past Due!";
$isPastDue = true;
}    
?>
<div class="container">
  <div class="inner">Transaction Date</div>
  <div class="inner">Due Date</div>
</div>
<h2 id="report">Notice</h2>

jquery:

$(document).ready(function(){
    $("<span>Past Due!</span>").insertAfter("#report");
});

What exactly I want is when the condition $isPastDue is true, I want to append/add a html tag next to #report h2. But it's not displaying. Any ideas? Help is much appreciated. Thanks.

0

4 Answers 4

3

Try this code

<?php 
if ( $duedatereservation < $today ) {
//echo "Past Due!";
$isPastDue = true;
}    
?>
<div class="container">
  <div class="inner">Transaction Date</div>
  <div class="inner">Due Date</div>
</div>
<h2 id="report">Notice</h2>
<?php if($isPastDue):?>
  <span>Past Due!</span>
<?php endif;?>

If your requirement this much, no need to add JavaScript.Just use only php.

Sign up to request clarification or add additional context in comments.

Comments

1

Try to do like this:

<?php
if($isPastDue) {
?>
<script>
    $(document).ready(function(){
        $("<span>Past Due!</span>").insertAfter("#report");
    });
</script>
<?php
}
?>

Comments

0

So, the main case is you want to execute the insertAfter script only if isPastDue is true. You can do it by echo the insertAfter script before closing } of your PHP if condition.

<?php
if($duedatereservation < $today){
    echo '<script>';
    echo '...
    echo '</script>';
}

Comments

0

No need to use JQuery for this. You can do this using PHP only

<h2 id="report">Notice <?php if($isPastDue){echo "<span>Past Due!</span>";} </h2>

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.