PHP:如何从用户那里获取输入、存储并显示回来?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4702672/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 14:02:56  来源:igfitidea点击:

PHP: How can I take an input from the user, store it, and display it back?

phphtmlforms

提问by Sanya

I've been trying to create some php code that would let me take input from a user using a text box. I want to then store that input (probably like 7 characters long) and then display it on a page in a list format ( eg 1 2 3 )

我一直在尝试创建一些 php 代码,让我使用文本框从用户那里获取输入。然后我想存储该输入(可能像 7 个字符长),然后以列表格式(例如 1 2 3 )在页面上显示它

I've been at this for hours reading tutorials and what not but everything I find only shows me the fwrite function and when I try to append I still end up deleting data.

我已经在这里阅读了几个小时的教程,但我发现的所有内容都只向我显示了 fwrite 函数,当我尝试追加时,我仍然最终删除了数据。

I would like the newest inputs to show up on top of the list if possible.

如果可能,我希望最新的输入显示在列表的顶部。

Can someone help me? I am really frustrated and I know almost no php.. kind of playing around with it and can't figure this out =/

有人能帮我吗?我真的很沮丧,我几乎不知道 php .. 有点玩弄它,无法弄清楚=/

I was thinking of storing the input from users in an array, then display the array.. But that doesn't work for me either.

我正在考虑将来自用户的输入存储在一个数组中,然后显示该数组..但这对我也不起作用。

回答by Fanis Hatzidakis

So you want to have a form, let the users enter text, then display all the text entered by all the users, sort of like a wall or guestbook.

所以你想要一个表单,让用户输入文本,然后显示所有用户输入的所有文本,有点像墙或留言簿。

I'm presuming you've managed to fetch the user's input by looking at $_POSTafter the form submission. To store it in a file without overwriting the existing file contents the easiest way is file_put_contentswith the special FILE_APPENDflag.

我假设您已经设法通过查看$_POST表单提交来获取用户的输入。要将其存储在文件中而不覆盖现有文件内容,最简单的方法是file_put_contents使用特殊FILE_APPEND标志。

Lets say your HTML form has a textbox with name="newData". Then, in the form submission target script:

假设您的 HTML 表单有一个带有name="newData". 然后,在表单提交目标脚本中:

//store all user input in a file called data.txt in the current directory
$filename = "./data.txt" ;
$newData = $_POST['newData'] . "\n" ;
file_put_contents($filename, $newData, FILE_APPEND);

//now fetch all data and display it
$lines = file($filename) ; 

echo '<ul>' ;

foreach ($lines as $line) {
    echo "<li>$line</li>" ;
}

echo '</ul>' ;

Get started like that to see the basics in action and then you can look into:

以这样的方式开始以查看实际操作的基础知识,然后您可以查看:

  • filtering user input so that you don't store and display any nasty stuff
  • storing the data file outside the web root so that it's not accessible via the browser
  • prettifying the output list
  • 过滤用户输入,这样你就不会存储和显示任何讨厌的东西
  • 将数据文件存储在 Web 根目录之外,以便无法通过浏览器访问
  • 美化输出列表

回答by jlmakes

If they're submitting the data via form, it will POST to the server; meaning you can use:

如果他们通过表单提交数据,它将 POST 到服务器;这意味着您可以使用:

<form action="target.php" method="post">
     <input type="text" name="username" value="" />
     <input type="submit" name="submit value="Submit" />
</form>

for the markup, and then for the PHP (on target.php):

对于标记,然后是 PHP(在 target.php 上):

<?php 
    if (isset($_POST['submit']) { //to check if the form was submitted
        $username= $_POST['username'];
    } 
?>

At this point you can then use <?php echo $username ?>anywhere you want to echo the data entered from the previous page.

此时,您可以<?php echo $username ?>在任何想要回显从上一页输入的数据的地方使用。

I'm no PHP expert either (I recommend the Lynda.com training videos, "PHP & MySQL Essential Training" and "PHP & MySQL Beyond The Basics" with Kevin Skoglund) but maybe this helps.

我也不是 PHP 专家(我推荐 Lynda.com 的培训视频、“PHP 和 MySQL 基本培训”以及 Kevin Skoglund 的“PHP 和 MySQL 基础知识”),但也许这会有所帮助。

回答by Kaushik Patil

try this for taking input from user in php

试试这个以在 php 中从用户那里获取输入

$variablname = fgets(STDIN); echo $variable;

$variablname = fgets(STDIN); 回声 $variable;

回答by Kaushik Patil

As said above, you can use $_POSTto get the data, however it is worth noting that if you are actually using this in a web application, you need to sanitize for XSS. You can do this quite simply with PHP's htmlspecialchars()

如上所述,您可以使用$_POST来获取数据,但是值得注意的是,如果您实际在 Web 应用程序中使用它,则需要对XSS进行消毒。你可以很简单地用 PHP 来做到这一点htmlspecialchars()

回答by Naatan

file_put_contents('/path/to/file','contents',FILE_APPEND);