Problems with jQuery append () and html ()

0

I have already seen another example here in the forum but it did not help me much by the amount of items inserted in div .

$carrinho = '<div class="carrinhoMais">
               <a href="carrinho.php">CARRINHO</a>
               R$ <label class="totalCarrinho">'.$phpUtil->formataMoeda($semiTotal).'</label>
             </div>';

echo '<script>$(".carrinho").html('.$carrinho.');</script>';

I'm not getting popular with div .

I've tried

echo '<script>$(".carrinho").append('.$carrinho.');</script>';

It also did not work.

I think it's the amount of html.

Give the following error.

index.php:214 Uncaught SyntaxError: Unexpected token <

Line 214

<script>$(".carrinho").html(
      <div class="carrinhoMais">
       <a href="carrinho.php">CARRINHO</a>
       R$ <label class="totalCarrinho">12,00</label>
     </div>
);</script>
    
asked by anonymous 03.06.2016 / 15:21

2 answers

0

Try this:

echo "<script>$('.carrinho').html('".$carrinho."');</script>";
    
03.06.2016 / 15:39
0

I did it.

Thank you!

<?php 
if(empty($_SESSION["carrinho"])) {    

    echo '<script>$(".carrinho").html("CARRINHO VAZIO")</script>';

 } else { 

    $semiTotal = 0.00;

    foreach ($_SESSION["carrinho"] as $carrinhoP) {

         $semiTotal += $carrinhoP["precoUnitario"] * $carrinhoP["quantidade"];
    }

    $carrinho = "<div class='carrinhoMais'><a href='carrinho.php'>CARRINHO</a>R$ <label class='totalCarrinho'>".$phpUtil->formataMoeda($semiTotal)."</label></div>";

    echo '<script>$(".carrinho").append("'.$carrinho.'");</script>';

}
?>

Another problem is that JS does not accept breaking lines as follows:

$carrinho = "
          <div class='carrinhoMais'>
            <a href='carrinho.php'>CARRINHO</a>
            R$ <label class='totalCarrinho'>".$phpUtil->formataMoeda($semiTotal)."</label>
          </div>";

It needs to be everything together

$carrinho = "<div class='carrinhoMais'><a href='carrinho.php'>CARRINHO</a>R$ <label class='totalCarrinho'>".$phpUtil->formataMoeda($semiTotal)."</label>/div>";
    
03.06.2016 / 15:40