简体   繁体   中英

echo HTML that contains data from MySQL (whitespace)

I am trying to output a dynamic table, that works with JQuery

Just to get rid of the "" signs I tried declaring another variable:

$rowname = $row["name"];

And then have that row name saved in the data-bubble, which would be used by JQuery.

echo '<tr id='.$current_row.' data-bubble={"part":"'.$rowname.'"}>';

The JQuery part:

    var bubData=jQuery.parseJSON($(this).attr("data-bubble"));
    console.log(bubData);
    $("#bubbleTable b.part").text(bubData.part);

So when I have a 'name' entry (NoSpace) that has no spaces inside it, everything works:

<tr id="1" data-bubble={"part":"NoSpace"}>

But whenever the name contains a space (Yes Space), this is what i get: Firefox:

<tr id="2" Space"}="" data-bubble={"part":"Yes">

IE:

<tr id="2" data-bubble='{"part":"Yes' Space"}="">

There has to be an easy way to get around this issue, but apparently my specialized English is not as good to find it myself. (Have been searching for quite a while now. Not the right things, I guess.)

you forget the double quote in data-bubble and the browsers interpret the content in different ways.

i suggest you to use printf/sprintf to format a string properly without double quote madness:

printf('<tr id="%d" data-bubble="{\'part\':\'%s\'}">', $current_row, $rowname);

output:

<tr id="1" data-bubble="{'part':'Yes Space'}>"

(based on comment) try to invert quoting to avoid problems:

printf("<tr id='%d' data-bubble='{\"part\":\"%s\"}'>", $current_row, $rowname);

output:

<tr id='1' data-bubble='{"part":"Yes Space"}'>

忽略tr字符串:

echo '<tr id='.$current_row.' data-bubble="{\"part\":\"'.$rowname.'\"}";

在斜杠后使用单引号,例如'\\

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM