Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何将pass用户输入传递到一个数组中,然后用PHP打印数据?_Php_Arrays - Fatal编程技术网

如何将pass用户输入传递到一个数组中,然后用PHP打印数据?

如何将pass用户输入传递到一个数组中,然后用PHP打印数据?,php,arrays,Php,Arrays,过去几天我一直在做一项作业。我一直在尝试将用户数据以数组形式传递到中,即用户将输入他们的课程id(如cpsc340),然后它将显示它,然后在同一行上,用户将键入cpsc 567左右,然后它将在原稿下面打印它,依此类推,这是我到目前为止所做的: </head> <body> <form method= "post";> <input type="text" name="courses[]" /> <br><br> <in

过去几天我一直在做一项作业。我一直在尝试将用户数据以数组形式传递到中,即用户将输入他们的课程id(如cpsc340),然后它将显示它,然后在同一行上,用户将键入cpsc 567左右,然后它将在原稿下面打印它,依此类推,这是我到目前为止所做的:

</head>
<body>
<form method= "post";>
<input type="text" name="courses[]" />
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
<?php 
$courses = $_POST['courses'];
foreach( $courses as $key => $n ) {
  echo $n;

  }
?>




有人能帮我一下,或者至少给我一些指导吗?

为了达到你的目的,我建议使用会话。请记住,这些不会永远存储,因此,如果您正在寻找一个永久性的解决方案,您应该使用数据库来存储它们

PHP不记得最后一个请求是什么,所以您需要将它存储在某个地方,因此我建议使用会话。您目前使用的是一个“HTML数组”,但只有一个,因此它实际上没有任何帮助。只需使用普通输入,并将其添加到会话中存储的数组中

请注意,
session\u start()
应该放在任何输出之前,因此只需将其放在文件的顶部即可

为了改进代码,我做了一些轻微的修改(检查是否设置了值,等等)


你的意思是每次提交表单时都要向数组中添加一个项目吗?是的,我希望这样用户可以添加一个课程,按submit,然后它会显示,然后冲洗并重复,每个课程都显示在上一个下,在某种程度上,用户会使数组增长这正是我需要的谢谢,我可以对课程和名称进行同样的描述吗?是的,你需要在课程中添加一个输入字段和另一个索引。好的,如果我想清除所有输入的数组怎么办?@RaheemHarris看一下我的编辑,它应该会显示你的两个问题:-)记住,如果这个答案解决了你的问题,请帮助未来的读者了解这一点,解决您的问题。
<?php
session_start();
?>
<!-- the start of your HTML goes here -->

<form method="POST">
    <input type="text" name="courses" />
    <br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

<?php 
// First we check if the form has been sent and we have a value
if (!empty($_POST['courses'])) {
    if (!isset($_SESSION['courses']))
        $_SESSION['courses'] = array(); // Initialize the array if it doesn't exist

    $_SESSION['courses'][] = $_POST['courses']; // Add the value to our array
}

// If there are values to show, print them!
if (!empty($_SESSION['courses'])) {
    foreach ($_SESSION['courses'] as $course) {
        echo $course."<br />";
    }
}
?>
<form method="POST">
    <input type="text" name="courses" />
    <input type="text" name="name" />
    <input type="text" name="description" />
    <br /><br />
    <input type="submit" name="submit" value="Submit">
</form>

<?php 
// First we check if the form has been sent and we have a value
if (!empty($_POST['courses'])) {
    if (!isset($_SESSION['courses']))
        $_SESSION['courses'] = array(); // Initialize the array if it doesn't exist

    // Add the value to our array
    $_SESSION['courses'][] = array("code" => $_POST['courses'],
                                    "name" => $_POST['name'],
                                    "description" => $_POST['description']); 
}

// If there are values to show, print them!
if (!empty($_SESSION['courses'])) {
    foreach ($_SESSION['courses'] as $course) {
        echo "Code: ".$course['code'].
              ", name: ".$course['name'].
              ", description: ".$course['description'].
              "<br />";
    }
}
?>
unset($_SESSION['courses']);