How to append add php conditions while replacing html text in jquery

936 Views Asked by At

basically , when document is ready , I would like to replace the td content with this html content that has some php conditions in it as shown below , but it's not working

<?php
 echo "<script type='text/javascript'>";
 echo "$(document).ready(function(){";
 ?>
 $('#redeem_freebie).html('<a class="btn" <? if (!$freebies->expired) {?>onclick="applyFreebieNow('<?=addslashes($dialog_copy)?>','<?=addslashes($freebies->get_started_link)?>','<?=$freebies->promo_code_id?>');"<? } ?> style="<?=$freebies->expired ? 'cursor: default;':''?>">Redeem Freebie &gt;&gt;</a>');
<?php 
 echo "});";    
 echo "</script>";
 echo "}";

?>
2

There are 2 best solutions below

0
On BEST ANSWER
<script type='text/javascript'>
$(document).ready(function(){
    $('#redeem_freebie').html('<a class="btn" <?php if (!$freebies->expired) {?>onclick="applyFreebieNow('<?php echo addslashes($dialog_copy); ?>','<?php echo addslashes($freebies->get_started_link)?>','<?php echo $freebies->promo_code_id?>');"<?php } ?> style="<?php echo $freebies->expired ? 'cursor: default;':''?>">Redeem Freebie &gt;&gt;</a>');
 });   
 </script>

Some hint : dont use <?= really not clear and your error was here <? } ?> <- php short tag

0
On

If you want to use some php function when document is ready, you can call an other php page using ajax.

example in jQuery :

$.ajax({
        url: 'yourpagewithfunction.php',
        type: "POST",
        data : {variable : value}
    }).done(function(msg) {
            alert(msg);
});