如何向变量添加输入


how to add input to a variable?

我有这个表格

<form action="process.php" method="post"> 
Team Name: <input type="text" name="teamname" /> 
<input type="submit" />
</form>

这是我的PHP代码

$teamname = $_POST['teamname'];
$namelist = "Lakers";

所以假设有两个人提交了他们的球队名称作为马刺和流浪者

那么我如何使名字列表像这样,并随着越来越多的人提交他们的团队名称而增长。

$namelist = "Lakers, Spurs, Rangers";

我已经用数组array_push做到了,但从技术上讲,我不能调用它们。

您需要将名称保存到文件/数据库中。变量保存在机器的内存中,并在脚本完成后删除。

$names = array(); // empty array
$names[] = $_POST['teamname']; // add the $_POST['teamname'] to the array
var_dump($names); // prints the names array.
// now the script done, and all the data in the variables will flush from the memory.

您可以使用数组来解决此问题

<?php
$teamname = $_POST['teamname'];
$namelist = $array("Lakers");
array_push($namelist,$teamname);
print_r($namelist);
?>

使用数据库是一个简单的解决方案。但是,如果您仍然不喜欢在这种情况下使用数据库,则可以将变量另存为会话或cookie变量。它将在页面刷新后保留。