Jquery append() and html() problems

Asked

Viewed 112 times

0

I’ve seen another example here on the forum but it didn’t 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 to div.

I’ve tried to

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

Also did not give.

I think it’s the amount of html.

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

2 answers

0

Try this:

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

0

I got.

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 line breaks as below:

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

It needs to be all together

$carrinho = "<div class='carrinhoMais'><a href='carrinho.php'>CARRINHO</a>R$ <label class='totalCarrinho'>".$phpUtil->formataMoeda($semiTotal)."</label>/div>";
  • Precisely, the problem was the lack of quotation marks around the html you were trying to insert.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.