使用调用JavaScript函数的php创建一个按钮
问题描述:
PHP
$output_string .= '<td><button id="changeButton" type="button" onclick="deleteAdress('. $row['adresses_id'] .', "'. $row['adresses_street'] .'");">Ta bort</button></td>';
Javascript
function deleteAdress(adressID, adressStreetName) {
alert(adressID);
}
I'm trying to create a button in php with onclick should make an alert box (for now). When i send numbers it works. But when one string contains text i stops working. Nothing. No alert box at all. I have also tried without the double quotes.
If you can see any problem please tell me. :)
答
Need to add escaped quotes
$output_string .= '<td><button id="changeButton" type="button" onclick="deleteAdress(\''. htmlspecialchars($row['adresses_id'], ENT_QUOTES) .'\', \''. htmlspecialchars($row['adresses_street'], ENT_QUOTES) .'\');">Ta bort</button></td>';
答
Try with following code
$output_string .= '<td><button id="changeButton" type="button" onclick="deleteAdress("'. $row['adresses_id'] .'", "'. $row['adresses_street'] .'");">Ta bort</button></td>';
答
You should just add quotes when calling the function:
onclick="deleteAdress(\''. $row['adresses_id'] .'\', "'. $row['adresses_street'] .'");