如何在 PHP 中获取像 prompt() 这样的用户输入?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22277633/
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
How to take user input like prompt() but in PHP?
提问by user3397425
How can I make a PHP input thing like prompt('example prompt')in javascript?
我怎样才能像prompt('example prompt')在 javascript 中那样做一个 PHP 输入的东西?
Not like a form, like a prompt().
不像表格,像prompt().
回答by Anonymous
You can't take input in the middle of php execution since it finishes before the page is actually shown to the user. However, you can get input using HTML and receive that using php. Here's a really basic example:
您不能在 php 执行过程中接受输入,因为它在页面实际显示给用户之前完成。但是,您可以使用 HTML 获取输入并使用 php 接收该输入。这是一个非常基本的示例:
<?php
echo $_POST['value'];
?>
<form method="post" action="">
<input type="text" name="value">
<input type="submit">
</form>
It takes the user input and reloads the page. Then, it echoes what the input was.
它接受用户输入并重新加载页面。然后,它回显输入的内容。
回答by Nabi K.A.Z.
Solution #1: Prompt for get input inside of anywhere code:
解决方案#1:提示在任何地方代码中获取输入:
<?php
echo "What do you want to input? ";
$input = rtrim(fgets(STDIN));
echo "I got it:\n" . $input;
Sample output:
示例输出:
# php test.php
What do you want to input? Hello, I'm here!
I got it:
Hello, I'm here!
Solution #2: If you want get input in firstly inline when run php:
解决方案#2:如果您想在运行 php 时首先内联输入:
<?php
$input = $argv[1];
echo "I got it:\n" . $input;
Sample output:
示例输出:
# php test.php "Hello, I'm here!"
I got it:
Hello, I'm here!
回答by Msfata
in php you better use form for inputs, however if you want you can use java script input and alert to read and display as follow.
在 php 中,您最好使用表单进行输入,但是如果您愿意,您可以使用 java 脚本输入和警报来读取和显示如下。
echo "<script>input=prompt('Enter your name !'); alert(input);</script>";

