Raheem Harris Raheem Harris - 24 days ago 5
PHP Question

How to pass an pass user input into an array however many times they want and then print the data in PHP?

I have an assignment that i have been working on for the past few days. I have been trying to pass in user data into in an array, i.e. the user would enter thier course id like cpsc340 and then it would display it, then on the same line, the user would type cpsc 567 or so and then it would print it underneath the original and so on and so forth, here is what i have so far:

</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;

}
?>


can someone please help or at least give me some direction?

Answer

To achieve what you want, I suggest using sessions. Keep in mind that these won't be stored forever, so if you're looking for a permanent solution, you should use a database to store them in.

PHP doesn't remember what the last request was, so you need to store it somewhere, hence my suggestion of using sessions. You currently use a "HTML array", but only one, so it doesn't really help anything. Just use a normal input, and add it to an array stored in the session.

Note that the session_start() should be placed prior to any output, so just put it at the top of your file.

I made some slight alterations as well to improve the code (checking if the values are set or not, etc)

<?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 />";
    }
}
?>

As per the comments:

If you wish to add more information, such as a name and description, you'd need to add the respective inputs and alter the session-array accordingly:

<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 />";
    }
}
?>

And then if you want to clear the array, you can simply unset it, using

unset($_SESSION['courses']);