I am trying to make a function in PHP that is like JavaScript's alert() command, but the OK button's onclick attribute is not working!
Here is my code:
<!DOCTYPE html>
<?php
function alert($title, $text) {
$html = '<div id="alert" style="background-color:lightGray; text-align:center; height:500px; width:500px; color: black; position:fixed; top: 50%; left:50%; margin-left:-250px; margin-top:-250px;">';
$html = $html . '<h1 style="background-color:red; border-radius: 15px;">'. $title . '</h1>' . $text;
$html = $html . '<br><br><button type="button" style="border-radius:25px; height:50px; width:100px; background-color:white; border:none;" onclick="document.getElementById(\"alert\").style.display=none">OK</button>';
echo $html;
}
alert('Testing', 'Testing <em>testing</em><b>123</b>');
?>
What is wrong? I get an "alert box" but the OK button doesn't work!
2 Answers 2
element.style.display = 'none' instead of just none
ps just want to be clear of javascript concept
2 Comments
' will give a conflict with the existing string delimiter. You have to escape it at least once.Change to:
document.getElementById(\"alert\").style.display=\"none\"
I guess this also shows that your current approach is quite terrible. Having to add all those escapes without code highlighting is annoying.
See if you can find a way to do it properly ;) (hint: you can).
document.getElementById().