json_encode 添加双引号而不是空格
json_encode adds double quotes instead of spaces
我正在尝试解决从 mysql 读取数据并将它们通过 php 传递到我的页面的问题。
我的任务是将从 mysql 读取的数据直接插入到 js 函数中。
代码是:
while($row = $query_ope->fetch_assoc()){
$data = array("str_id" => $row['Id'],
"str_rag_sociale" => $row['Rag_Sociale'],
"str_indirizzo" => $row['Indirizzo'],
"att_luogo" => json_decode($row['Comune']),
"str_cap" => $row['Cap'],
"str_reparto" => $row['Reparto']
);
$content .= '<tr class="cal_body">'.
<td class="btn_icon icon icon-setup" onclick=modUt('.json_encode($data).')></td>'.
'</tr>';
现在的问题是来自数据库字段中包含空格的数据(例如,当 $row['Comune'] = "some text" 时)被解析如下:
onclick=modUt({...,"att_luogo" : "some" text","str_cap"....})
在第一个单词 some 之后加上双引号,这是一个问题,因为我的 js 函数失败了。
我正在疯狂地试图理解为什么。
您必须 html 编码您的 html 属性
$content .= '<tr class="cal_body">
<td class="btn_icon icon icon-setup" onclick="'.htmlspecialchars('modUt('.json_encode($data).')').'"></td>'.
'</tr>';
我正在尝试解决从 mysql 读取数据并将它们通过 php 传递到我的页面的问题。 我的任务是将从 mysql 读取的数据直接插入到 js 函数中。 代码是:
while($row = $query_ope->fetch_assoc()){
$data = array("str_id" => $row['Id'],
"str_rag_sociale" => $row['Rag_Sociale'],
"str_indirizzo" => $row['Indirizzo'],
"att_luogo" => json_decode($row['Comune']),
"str_cap" => $row['Cap'],
"str_reparto" => $row['Reparto']
);
$content .= '<tr class="cal_body">'.
<td class="btn_icon icon icon-setup" onclick=modUt('.json_encode($data).')></td>'.
'</tr>';
现在的问题是来自数据库字段中包含空格的数据(例如,当 $row['Comune'] = "some text" 时)被解析如下:
onclick=modUt({...,"att_luogo" : "some" text","str_cap"....})
在第一个单词 some 之后加上双引号,这是一个问题,因为我的 js 函数失败了。 我正在疯狂地试图理解为什么。
您必须 html 编码您的 html 属性
$content .= '<tr class="cal_body">
<td class="btn_icon icon icon-setup" onclick="'.htmlspecialchars('modUt('.json_encode($data).')').'"></td>'.
'</tr>';