PHP - 根据用户单击的按钮返回真或假

Posted

技术标签:

【中文标题】PHP - 根据用户单击的按钮返回真或假【英文标题】:PHP - Return true or false depending on which button user clicked 【发布时间】: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 和逗号而不是连接字符串 【参考方案1】:

还有另一种不那么混乱的方式来制作你的 html

$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.= '<h1 style="background-color:red; border-radius: 15px;">'. $title . '</h1>';
$html.= '<span style="font-size: 20px;">The page at ' . $_SERVER['SERVER_NAME'] . ' says...</span><br><br>' . $text;
$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.= '<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;  

编辑 - 要在 php 中从用户那里获取值,您必须使用 ajax、表单或 _GET 协议。请注意,一旦出现在屏幕上,您的 html 代码就不再链接到任何 php 脚本。您的 php 函数在调用期间执行,而 javascript 函数在客户端执行。您不能从页面调用函数并在 php 函数中使用返回值。您可以做的是在单击按钮时重新加载页面,并在您的 url 中使用您想要的值。注意 onclick 属性。

<button type="button" style="border-radius:25px; height:50px; width:100px; background-color:lightGray; border:1px solid black;" onclick="window.location.href='yourscript.php?confirm=false'">Cancel</button>
<button type="button" style="border-radius:25px; height:50px; width:100px; background-color:lightGray; border:1px solid black;" onclick="window.location.href='yourscript.php?confirm=true'">OK</button>

此外,您的按钮可以被锚替换,就像这样(它们可以具有与您的按钮完全相同的样式):

<a href="yourscript.php?confirm=false">cancel</a>
<a href="yourscript.php?confirm=true">OK</a>

你可以像这样在你的 php 脚本中检索值:

$confirm = (isset($_GET['confirm']) ? $_GET['confirm'] : '');

【讨论】:

我只想在 PHP 中有一个返回值。如果他们单击“确定”,则返回 true。如果他们点击“取消”,则返回 false。 退出 php 模式而不是回显所有 HTML 怎么样?然后你不需要引号或反斜杠,你可以调用 php 来回显变量......就像模板语言一样。

以上是关于PHP - 根据用户单击的按钮返回真或假的主要内容,如果未能解决你的问题,请参考以下文章

如何检查 jQuery 查找返回真或假? [复制]

Javascript 函数 img.onload 不返回任何内容(真或假)

我应该使用 Any() 还是 Count() ?哪个更快?如果 IEnumerable 对象中存在任何数据,两者都会返回相同的输出(真或假)吗? [复制]

“返回 x == y”是啥意思? [复制]

从 0 到 20 的随机数生成器,用户输入一个可以为真或假的数字

启用和禁用一个组件/HTML 中的所有元素