vendredi 8 mai 2020

PHP: Getting checked values from a checkbox form?

I am making a program where a user picks songs to add from a form, and after he/she picks them and hits submit they are redirected to a page that displays the songs they picked in a playlist. Here is my form that lets a user add songs:

<h1>Pick a Song to Add to the Playlist</h1>
<form action="playlistSongs.php" method="post">
<input type="checkbox" id="s01" name="song" value="song_name">
<label for="song_name"> He is the Same</label><br>
<input type="checkbox" id="s02" name="song" value="song_name">
<label for="song_name"> 80's Films</label><br>
<input type="checkbox" id="s03" name="song" value="song_name">
<label for="song_name"> All Time Low</label><br>
<input type="checkbox" id="s04" name="song" value="song_name">
<label for="song_name"> New York Soul - Pt.II</label><br>
<input type="checkbox" id="s05" name="song" value="song_name">
<label for="song_name"> Fashion</label><br>
<input type="checkbox" id="s06" name="song" value="song_name">
<label for="song_name"> Maybe IDK</label><br>
<input type="checkbox" id="s07" name="song" value="song_name">
<label for="song_name"> Woke Up</label><br>
<input type="checkbox" id="s08" name="song" value="song_name">
<label for="song_name"> Overwhelming</label><br>
<input type="checkbox" id="s09" name="song" value="song_name">
<label for="song_name"> Weight of the World</label><br>
<input type="checkbox" id="s10" name="song" value="song_name">
<label for="song_name"> The Good in Me</label><br>
<input type="checkbox" id="s11" name="song" value="song_name">
<label for="song_name"> Morning in America</label><br>
<input type="checkbox" id="s12" name="song" value="song_name">
<label for="song_name"> iRobot</label><br>
<input type="checkbox" id="s13" name="song" value="song_name">
<label for="song_name"> Guillotine</label><br>
<input type="checkbox" id="s14" name="song" value="song_name">
<label for="song_name"> Munny Right</label><br>
<input type="checkbox" id="s15" name="song" value="song_name">
<label for="song_name"> Carry Your Throne</label><br>
<input type="checkbox" id="s16" name="song" value="song_name">
<label for="song_name"> Pre-Occupied</label><br>
<input type="checkbox" id="s17" name="song" value="song_name">
<label for="song_name"> Human</label><br>
<input type="checkbox" id="s18" name="song" value="song_name">
<label for="song_name"> Run Wild</label><br>
<input type="checkbox" id="s19" name="song" value="song_name">
<label for="song_name"> A Haunted House</label><br>
<input type="checkbox" id="s20" name="song" value="song_name">
<label for="song_name"> Jungle</label><br>
<input type="checkbox" id="s21" name="song" value="song_name">
<label for="song_name"> Simple Sweet</label><br>
<input type="checkbox" id="s22" name="song" value="song_name">
<label for="song_name"> An Immigrant</label><br>
<input type="checkbox" id="s23" name="song" value="song_name">
<label for="song_name"> Ooh</label><br>
<input type="checkbox" id="s24" name="song" value="song_name">
<input type="Submit" name = "submit">
</form>

Script that posts the selected songs and displays them:

<?php

session_start();

if( isset($_SESSION["logged"]) && $_SESSION["logged"] ){
    echo $_SESSION["ID"] . " is a valid user";
    echo "<br />";
    echo "<br />";
    echo "PLAYLIST SONGS:";
    echo "<br />";
    echo "<br />";

    $server = "127.0.0.1:33067";
    $username = "root";
    $password = "";
    $db = "music";

    // connecting to the database
      $conn = new mysqli("127.0.0.1" ,"root", "" , "music", 33067,"/Users/Kasia/Desktop/DevDesktop/mysql/data/mysql.sock");
    if( $conn->connect_error ){
        die( "Connection failed: " . $conn->connect_error );
    }

if(isset($_POST['submit'])){

    if(!empty($_POST['song_name'])) {

        foreach($_POST['song_name'] as $value){
            echo "Song: ".$value.'<br/>';
        }

    }

}

The code is not working. Can somebody point out where I made a mistake? I am a beginner and this stuff doesn't come easy to me. Thanks




Aucun commentaire:

Enregistrer un commentaire