PHP-根据用户单击的按钮返回true或false
PHP - Return true or false depending on which button user clicked
我正在尝试制作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?
还有另一种不那么容易混淆的方法来制作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;
EDIT-要在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'] : '');
相关文章:
- 对于loop.if-仅在经过所有间隔后返回true
- 如果函数返回True,则显示Javascript按钮
- Javascript If else 只返回 TRUE 或只返回 FALSE
- 过滤器返回true或false
- 我对“;返回true"嵌套函数内部;t工作
- 如何在成功输入时使“返回TRUE”
- 检查一个元素是否有一个类与另一个类总是返回true
- JavaScript函数未返回true
- 如果URL中有任何字符串变体匹配,则返回true
- 如何使以下函数返回true或false
- 使用“in”关键字检查 0 始终返回 true
- hasOwnProperty 在对照父对象属性进行检查时返回 true
- 为什么 isNaN 总是返回 true
- _.数组中的每个查找值都返回 true 或 false.使用下划线 JS
- 错误地返回true
- 为什么asp:LinkButton服务器端事件没有'当clientClick返回true时,t激发
- 阻止表单提交 javascript 为什么有一个明显的错误语句返回 true
- Node js命令器args返回true而不是值
- 如何将html表单与php链接,以便在例如单击按钮时返回true
- 从OnClientClick调用函数时,在JavaScript中返回true