0

I am working on a php project that consists of an html form that allows the user to submit the bits of text, a song name, composer, and artist. Once the user fills in the form and clicks submit, the data should be stored and allow the form to be filled in again until the user pushes another button which shows all of the data that has been submitted. I have so far thought of using arrays, but I am not sure how that would work with multiple form submissions sending to the same array. Any help would be greatly appreciated.

    <html>
      <head>
      </head>

      <body>
        <form method="post">
          Name of song: <input type="text" name="songName"><br>
          Composer: <input type="text" name="composer"><br>
          Artist/Group: <input type="text" name="artist"><br>
          <input type="submit" name="submit">
        </form>
      </body>


      <?php
        if (!empty($_POST['submit'])) {
          //Submit the data into the array or something here
        }
      ?>
    </html>
4

1 回答 1

1

Certainly, try this and see what happens:

<?php
    session_start();

    // Initialize an array for answers
    if (!isset($_SESSION['answers']))
        $_SESSION['answers'] = array();
?>
<html>
  <head>
  </head>

  <body>
    <form method="post">
      Name of song: <input type="text" name="songName"><br>
      Composer: <input type="text" name="composer"><br>
      Artist/Group: <input type="text" name="artist"><br>
      <input type="submit" name="submit">
    </form>
  </body>


  <?php
    if (!empty($_POST['submit'])) {
        // Push the posted data into the session array
        $_SESSION['answers'][] = $_POST;
    }

    // Display the data now
    foreach($_SESSION['answers'] as $array) {
        echo "Name of song: {$array['songName']}<br>";
        echo "Composer: {$array['compose']}<br>";
        echo "Artist/Group: {$array['artist']}<br><hr>";
    }
  ?>
</html>

Note: SESSIONS only remain until the user logs off or they time out. For persistance over long periods, you need to use a database such as MySQL to store the answers

于 2013-10-14T02:24:59.937 回答