Append a string with quotes from PHP to jQuery

tim99

i want to append a sting with quotes (eg. < span class='MyClass'> ) to some jQuery Elements, which are stored in a database. This snippet works as i want.

<script>
  $(document).ready(function() {
    <?php 
      $result = mysql_query("SELECT concat(year(`wann`), lpad(month(`wann`),2,0), lpad(day(`wann`),2,0)) as wann, `wieviel` FROM `zecken`");
      while($row = mysql_fetch_array($result)) {
        for($i=1; $i <= $row["wieviel"]; $i++) {
    ?>

$('#<?php echo $row["wann"]; ?>').append("<span class='descr bg-danger'>1</span>");

    <?php 
        } 
      } 
    ?>
  });
</script>

I just want to write in completely in PHP, and it also works if i delete the css-classes "descr bg-danger" from the span. Without css-classes my solution would be:

<script>
  $(document).ready(function() {

    <?php 
      $result = mysql_query("SELECT concat(year(`wann`), lpad(month(`wann`),2,0), lpad(day(`wann`),2,0)) as wann, `wieviel` FROM `zecken`");
      while($row = mysql_fetch_array($result)) {
        for($i=1; $i <= $row["wieviel"]; $i++) {

          // How to add a class to this appended span ?????
          echo "$('#".$row["wann"]."').append('<span >1</span>'); ";        
        } 
      } 
    ?>

  });
</script>

If i have to escape some quotes, how do i have to do this? It's like i would need a third pair of different quotes for the span: ;)

echo "$('#".$row["wann"]."').append('<span class='''descr bg-danger'''>1</span>'); ";
user94559
echo "$('#".$row["wann"]."').append('<span class=\\'descr bg-danger\\'>1</span>'); ";

or, as @Andi North mentioned:

echo "$('#".$row["wann"]."').append('<span class=\"descr bg-danger\">1</span>'); ";

(The difference between the two is that one outputs class=\'... and one outputs class=".... Either should be perfectly valid JavaScript.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Jquery append string from array

From Dev

Cannot remove double quotes from string PHP

From Dev

JQuery - append multiple columns to a table from PHP

From Dev

Importing PHP string with quotes

From Dev

How to remove double quotes from default string in javascript/Jquery

From Dev

jquery html append outputs excessive quotes

From Dev

Jquery append to PRE tag without quotes

From Dev

Remove quotes when reading from sessionStorage and sending to php using jquery

From Dev

Scheme string-append with double quotes

From Dev

sql Append for value with single quotes - String Concatenation

From Dev

PHP - Regular Expression to Remove Single Quotes From Beginning and End of a String

From Dev

php - get content from second pair of quotes in string

From Dev

how to omit double quotes and array brackets from a string in php

From Dev

jQuery append php html string returned by Codeigniter's controller

From Dev

replace single quotes and double quotes from a string

From Dev

Append a php file with jquery

From Dev

Append query string - PHP

From Dev

need to append to a string in php

From Dev

PHP: Work with a large string with quotes

From Dev

Append to an array from a string

From Java

Remove quotes from String in Python

From Dev

Removing the double quotes from a string

From Dev

Remove quotes from String in Python

From Dev

Extract word in quotes from string

From Dev

JQuery append() adding quotes and newlines and I don't know why

From Dev

how to use string builder to append double quotes and 'OR' between elements

From Dev

Which is the best way to append single quotes for a String in java

From Dev

Append table using jQuery with php

From Dev

How to append String with comma in php

Related Related

HotTag

Archive