【发布时间】:2013-11-28 20:09:54
【问题描述】:
我正在尝试制作 JavaScript 的 confirm() 命令的 PHP 版本:
<!DOCTYPE html>
<?php
function confirm($title, $text) {
$html = '<div id="alert" style="background-color:white; text-align:center; height:400px; width:300px; color:black; position:fixed; top: 0; left:50%; margin-left:-150px; border:1px solid black; box-shadow: 5px 5px 10px gray;">';
$html = $html . '<h1 style="background-color:red; border-radius: 15px;">'. $title . '</h1>';
$html = $html . '<span style="font-size: 20px;">The page at ' . $_SERVER['SERVER_NAME'] . ' says...</span><br><br>' . $text;
$html = $html . '<br><br><button type="button" style="border-radius:25px; height:50px; width:100px; background-color:lightGray; border:1px solid black;" onclick="this.parentNode.style.display=\'none\'">OK</button>';
$html = $html . '<button type="button" style="border-radius:25px; height:50px; width:100px; background-color:lightGray; border:1px solid black;" onclick="this.parentNode.style.display=\'none\'">Cancel</button>';
echo $html;
}
?>
如何根据用户单击的按钮返回 true 或 false?
【问题讨论】:
-
如果不提交
form+button type=submit或javascript Ajax 调用,则无法单独使用php 返回 -
你的函数显示了一些 html 代码。为什么要返回 true 或 false?
-
题外话:为了提高性能,使用
echo和逗号而不是连接字符串
标签: javascript php html user-defined-functions