echo "<button onclick=\"check(".$user['brandname'].")\">".$user['brandname']."</button>";
生成的 HTML 為 onclick=check(品牌名),缺少引號,JS 引擎將參數視為變量而非字符串,導致報錯無反應。
echo "<button onclick=\"check('".$user['brandname']."')\">".$user['brandname']."</button>";
生成 onclick=check('品牌名'),雙重轉義確保單引號輸出到 HTML 屬性中。
json_encode 自動處理轉義:onclick=\"check(".json_encode($user['brandname']).")\"。