PHP 中 Alert() 和 Prompt() 的等价物
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30388997/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Equivalent of Alert() and Prompt() in PHP
提问by user4757174
In JavaScript, we have Alert() and Prompt() which open up a popup box for the user.
在 JavaScript 中,我们有 Alert() 和 Prompt() 为用户打开一个弹出框。
Is there an equivalent for PHP?
$Get_['asdf']
is one way to get user input... any others?
PHP 是否有等价物?
$Get_['asdf']
是获取用户输入的一种方法......还有其他方法吗?
Also, one more question. Is it a requirement that PHP always be executed all at once? Or can it be like JavaScript, where it waits for the user input (e.g. popup box), then executes the rest of the code after that.
另外,还有一个问题。是否要求始终同时执行 PHP?或者它可以像 JavaScript 一样,等待用户输入(例如弹出框),然后执行其余代码。
回答by SantanuMajumdar
PHP is a server side language, it can't do alert messages on the client side. But you can use javascript within the php to do the alert.
PHP 是一种服务器端语言,它不能在客户端发出警报消息。但是您可以在 php 中使用 javascript 来执行警报。
<script type="text/javascript">
window.alert("Hi There, I am the Alert Box!")
</script>
For Prompt you can do something like this -
对于提示,您可以执行以下操作 -
<?php
//prompt function
function prompt($prompt_msg){
echo("<script type='text/javascript'> var answer = prompt('".$prompt_msg."'); </script>");
$answer = "<script type='text/javascript'> document.write(answer); </script>";
return($answer);
}
//program
$prompt_msg = "Please type your name.";
$name = prompt($prompt_msg);
$output_msg = "Hello there ".$name."!";
echo($output_msg);
?>
回答by Joel Hinz
Nope, there is no equivalent. All php is executed on the server-side only. Unless you're using it at the command-line, which I doubt.
不,没有等价物。所有 php 都只在服务器端执行。除非你在命令行中使用它,我对此表示怀疑。
It also cannot wait for user input like javascript, like you wanted. Sorry. You'll have to use ajax for that.
它也不能像您想要的那样等待像 javascript 这样的用户输入。对不起。为此,您必须使用 ajax。
回答by Ivan Shumakov
That's it:
就是这样:
$shouldProceed = readline('Do you wanna proceed?(y/n): ');
if (strtolower(trim($shouldProceed)) == 'n') exit;
proceed();