Click here to Skip to main content
15,884,629 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hay I want to use my PHP code in a Javascript. Here is my code:

JavaScript
     <script>
    
        var clicked = false;
        val="<?php echo $final; ?> € <?php if ($proz > 0) {echo "<span class=\"badge 
             badge-success\"> $proz %</span>";} else { echo "<span class=\"badge badge- 
             success\"> $minus_cal %</span>";}?>";
        function toggle(){
            if(!clicked){
                clicked = true;
                document.getElementById("btn").innerHTML = "Shown";
                document.getElementById("output").innerHTML = "***";
            } else{
                clicked = false;
                document.getElementById("btn").innerHTML = "Hide";
                document.getElementById("output").innerHTML = val;
            }
        }
</script>


What I have tried:

I think that I know where the issue is. It's the classes inside the span Tag.I tried also so, here it's work but my span is now not formatted.
JavaScript
     <script>
    
        var clicked = false;
        val="<?php echo $final; ?> € <?php if ($proz > 0) {echo "<span> $proz % 
             </span>";} else { echo "<span> $minus_cal %</span>";}?>";
        function toggle(){
            if(!clicked){
                clicked = true;
                document.getElementById("btn").innerHTML = "Shown";
                document.getElementById("output").innerHTML = "***";
            } else{
                clicked = false;
                document.getElementById("btn").innerHTML = "Hide";
                document.getElementById("output").innerHTML = val;
            }
        }
</script>


Thanks for any Help!
Wishes Aaron.
Posted
Updated 15-Sep-21 4:24am

1 solution

Try using single quotes around your Javascript variable:
PHP
let val = '<?php echo $final; ?> € <?php if ($proz > 0) {echo "<span class=\"badge badge-success\"> $proz %</span>";} else { echo "<span class=\"badge badge-success\"> $minus_cal %</span>";}?>';
 
Share this answer
 
Comments
Aaron Gladun 15-Sep-21 10:29am    
Thanks Bro, Its work fantastically.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900