0

I am currently trying to create a website for my church in which we can select songs for the songlist each week. We choose 6 songs and I am wanting to set up multiple forms one one page to allow different criteria to be selected and each song to be pulled individually and then I will create a master submit at the bottom which will pull the data from all 6 forms to generate the final data.

Below is sample data of just entering a song and artist on a 2 form test, but the problem is when I submit 1, the other one empties. I need all the data to stay so that once all 6 are filled in the master submit can grab all the data that has been posted. Please shed any insight you can.

<?PHP           
        $Artist1 = $Song1 ="";  
    if (($_SERVER["REQUEST_METHOD"] == "POST") and isset($_POST["submit1"]) )
    {   
         if (empty($_POST["Artist1"]))
         {
            $Artist1 = "";
         } else 
         {
            $Artist1 = test_input($_POST["Artist1"]);
         }

         if (empty($_POST["Song1"]))
         {
            $Song1 = "";
         } else 
         {
            $Song1 = test_input($_POST["Song1"]);
         }           
    }
        ?>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            Artist: <input type="text" name = "Artist1" value="<?php echo $Artist1;?>">
            <br>
            Song: <input type="text" name = "Song1" value="<?php echo $Song1;?>">
            <br>
            <input type = "submit" name = "submit1" value="Submit1">
        </form>

        <?php

        echo "<h2>Your song </h2>";
        echo $Artist1;
        echo "<br>";
        echo $Song1;        
        ?>
<?PHP

        $Artist2 = $Song2 ="";
    if (($_SERVER["REQUEST_METHOD"] == "POST") and isset($_POST["submit2"]) )
    {   
         if (empty($_POST["Artist2"]))
         {
            $Artist2 = "";
         } else 
         {
            $Artist2 = test_input($_POST["Artist2"]);
         }

         if (empty($_POST["Song2"]))
         {
            $Song2 = "";
         } else 
         {
            $Song2 = test_input($_POST["Song2"]);
         }       
    }
        ?>
        <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
            Artist: <input type="text" name = "Artist2" value="<?php echo $Artist2;?>">
            <br>
            Song: <input type="text" name = "Song2" value="<?php echo $Song2;?>">
            <br>
            <input type = "submit" name = "submit2" value="Submit2">
        </form>

        <?php

        echo "<h2>Your song </h2>";
        echo $Artist2;
        echo "<br>";
        echo $Song2;

        function test_input($data) {
        $data = trim($data);
        $data = stripslashes($data);
        $data = htmlspecialchars($data);
        return $data;
        }

        ?>
ArK
  • 20,698
  • 67
  • 109
  • 136
  • Just from your question, and without looking at the code, this sounds like a multi-page form setup? – Nathan Robb Sep 11 '16 at 05:08
  • 1
    Oh, no, all 6 forms are all on the SAME page. Why not just make a single form? – Nathan Robb Sep 11 '16 at 05:09
  • It's not a write-me-a-code website – Luke Sep 11 '16 at 05:14
  • @Luke I'm not sure what you mean. – Nathan Robb Sep 11 '16 at 05:30
  • @NathanRobb I'm fairly new to this and am piecing stuff together to try and accomplish what I'm wanting and didn't see anything that met my criteria already created. I thought that I would need different forms because I'm going to be pulling additional information from an mysqli database based on what's imported into the form and thought that I'd need to do it through different forms to show the results of each pull. – Tetsusaiga Sep 11 '16 at 05:46
  • Well, from what I'm seeing here, you have `Song1` and `Artist1`, `Song2` and `Artist2`, etc but all in separate `
    `s. What I'm saying is why not just put them into one `
    `? I'm not seeing any differences between the form properties (they are all `POST` and post to the same place).
    – Nathan Robb Sep 11 '16 at 05:51
  • What do you mean you will be pulling information from MySQL? By the way, `mysqli_` are the functions for PHP to connect to a MySQL database. `mysqli` stands for "mysql improved" if I'm not mistaken, referring to the old `mysql_` functions which are now deprecated and removed in PHP7 – Nathan Robb Sep 11 '16 at 05:54
  • I shouldn't say "the" functions, there are other ways, such as PDO, but that's not the point of this thread :) – Nathan Robb Sep 11 '16 at 05:55
  • Oh, by "pulling information from MySQL", you mean you will be pulling already-existing song data based on the titles that you get from the form(s)? – Nathan Robb Sep 11 '16 at 05:56
  • @NathanRobb correct. When you enter data into the form such as the artist name and the song. It will pull data back such as when the last time it was played, and any notes for the song that need to be known. I will try to do it all in one form and see if it will achieve what I'm trying for. Thanks again. – Tetsusaiga Sep 11 '16 at 06:05
  • If I'm understanding your situation correctly, then what I would do is make a sort of dynamic form where the user can add songs (and add fields to the form dynamically) like `Song_i` and `Artist_i` where `i` is an incremented number. OR I would do a session-based thing where the user has a "song list" and they can click a button to add more songs, that button would take you to a form where you had only the 2 fields, `Song` and `Artist` and you would then store that data in the user's session (or database, whatever). The idea being that they would have a list that they can add and subtract from – Nathan Robb Sep 11 '16 at 06:05
  • Option one is a more complex form, option 2 is a more complex system of pages. Both are fairly simple though, and given time and resources, I have no doubt you would be able to to either :) Does this make sense? – Nathan Robb Sep 11 '16 at 06:06

1 Answers1

0

In HTML if you have a form with an action property when you click on submit the browser will do a POST to the action webpage you set. There it's no way to do what you need in that way.

You have three solutions:

  1. Join all the forms in a single form
  2. Use iframes, on each iframe you should have one form for one song.
  3. Use Ajax to send songs when the user press the button on each song form like this way
Community
  • 1
  • 1
NetVicious
  • 3,848
  • 1
  • 33
  • 47