I've searched about escaping the parameter inside the JS function and I've tried lots of suggestions but none works. Here is the code:
echo '<a class="btn btn-xs btn-danger" onclick="delete('.$fetch['bicc'].')"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
I always get this error:
SyntaxError: identifier starts immediately after numeric literal
delete(123456789_7_ZY7)
您需要添加引号,您的param是字符串类型:
echo '<a class="btn btn-xs btn-danger" onclick="delete(\''.$fetch['bicc'].'\')"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
You forgot about \\"
onClick="delete(\"'.$fetch['bicc'].'\")"><span class="glyphicon glyphicon-remove"></span> Remove</a>
So it should be like:
echo '<a class="btn btn-xs btn-danger" onclick="delete(\"'.$fetch['bicc'].'\")"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
As a rule of thumb, if you want to use PHP variables in your JavaScript code, you need to wrap them in json_encode
:
echo '<a class="btn btn-xs btn-danger" onclick="delete('.json_encode($fetch['bicc']).')"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
json_encode
is able to take most PHP variables (strings, arrays, associative arrays) and correctly encode them as their JavaScript counterparts (eg, adding quotes for strings).
您应该添加引号,因为参数是字符串
echo "<a class=\"btn btn-xs btn-danger\" onclick=\"delete(\"${fetch['bicc']}\")\"><span class=\"glyphicon glyphicon-remove\"></span> Remove</a>";
$fetch['bicc'] has string type value?
if yes:
echo '<a class="btn btn-xs btn-danger" onclick="delete(\"'.$fetch['bicc'].'\")"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
$data = str_replace('_', ' ', $fetch['bicc']);
echo '<a class="btn btn-xs btn-danger" onclick="delete('.$data.')"><span class="glyphicon glyphicon-remove"></span> Remove</a>'
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.