Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

PHP- Drop down menu help

Options
  • 03-04-2012 8:44pm
    #1
    Registered Users Posts: 2,589 ✭✭✭


    Hi guys, i have a bit of a PHP problem and would appreciate a bit of help. I am currently doing a project in college and it involves PHP, which i am a bit dodgy on.

    Basically we have to create a HTML page, with a drop box menu showing 5 music albums, and a Submit button. I have done this much:
    <html>

    <title>Album List</title>

    <form method="post" action="<?php echo $PHP_SELF;?>">

    <select name="albums">
    <option value="1">Appetite For Destruction</option>
    <option value="2">Out Of Exile</option>
    <option value="3">West Ryder Pauper Lunatic Asylum</option>
    <option value="4">One By One</option>
    <option value="5">Master Of Puppets</option>

    <p><input type="submit" value="Submit" /></p>
    </select>
    </form>


    </body>
    </html>

    Now, when you pick an album name and hit submit, it should bring you to a page showing all the album info, track list, etc. (I have all this info from a previous project). But where im stuck is for the PHP code needed to link the HTML drop box page.

    Basically, at the end i should have 1 HTML file (dropbox), 1 PHP file (album info) and 1 CSS file for styles (which im ok with.)

    Any help would be appreciated.


Comments

  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    I presume there'll be no database used here.

    At the moment you're using
    <?php echo $PHP_SELF;?>
    

    ...for the 'action' attribute. 'Action' is the target url/file, i.e. where the form gets submitted to.

    Currently it submits the current page itself. Now you could do it this way, and just have some conditions which display different information depending on whether the form is submitted or not.

    But for simplicity best to separate it, create a new file called search_results.php or somethin. Then get rid of the PHP from the Action, and instead put in that file name.

    Then in the .php file you use $_POST to retrieve the data submitted by the form. You use the 'name' of the form to retrieve this, i.e.
    <?php
    $chosen_album = $_POST['albums'];
    $album_info = '';
    if($chosen_album==1) {
    //appetite...
    $album_info = 'some stuff goes here.... You can include html tags too';
    }
    elseif($chosen_album==2) {
    //out of exile...
    $album_info = 'some stuff goes here.... You can include html tags too';
    }
    etc
    ?>
    

    You could use a switch statement there instead.

    I presume you've got PHP on whatever server you're using for this project? If not the XAMPP is good for installing PHP on a local server.

    Dunno if the code above is perfect, should get you going though.


  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    Cheers for the reply. The code you've given looks like the way to go. I was thinking something along these lines alright, just couldnt figure it out.

    As for a server I will probably go for Xampp or Wamp.
    Xampp seems a bit handier tho so might go for that

    So instead of this:
    <form method="post" action="<?php echo $PHP_SELF;?>">

    youre saying replace it with:
    <form method="post" action="<chosen_album.php>">


  • Closed Accounts Posts: 27,857 ✭✭✭✭Dave!


    remove the <> from around it, so:
    <form method="post" action="chosen_album.php">
    

    The form is just HTML, no need to include any PHP in it. You're just telling it where to submit the form to. The PHP is executed on the server.


  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    so here's what i have for my PHP code (just have info for first 3 albums on list to test it out):
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"&gt;
    <html>
    <head><title>Chosen Album</title></head>
    <?php
    $chosen_album = $_POST;
    $album_info = '';


    if ($chosen_album== 1) {
    //appetite for destruction
    $album_info =
    '<p class="album_title">Appetite For Destruction</p>
    <p class="artist">Artist: Guns N Roses</p>

    <p class="track_list">Track List:</p>
    <ol class="list">
    <li>Welcome to the jungle</li>
    <li>Its so easy</li>
    <li>Nightrain</li>
    <li>Out ta get me</li>
    <li>Mr Brownstone</li>
    <li>Paradise city</li>
    <li>My Michelle</li>
    <li>Think about you</li>
    <li>Sweet Child o mine</li>
    <li>Youre crazy</li>
    <li>Anything goes</li>
    <li>Rocket queen</li> </ol>


    <iframe width="420" height="315" src="http://www.youtube.com/embed/i_gQOfF0Ymo&quot; frameborder="0" ';
    }



    elseif($chosen_album==2) {
    //out of exile
    $album_info = '<p class="album_title">Out Of Exile</p>
    <p class="artist">Artist: Audioslave</p>

    <p class="track_list">Track List:</p>
    <ol class="list">
    <li>Your Time Has Come</li>
    <li>Out Of Exile</li>
    <li>Be Yourself</li>
    <li>Doesnt Remind Me</li>
    <li>Drown Me Slowly</li>
    <li>Heavens Dead</li>
    <li>The Worm</li>
    <li>Man Or Animal</li>
    <li>Yesterday To Tomorrow</li>
    <li>Dandelion</li>
    <li>#1 Zero</li>
    <li>The Curse</li>
    <li>Like A Stone</li>
    </ol>
    <iframe width="420" height="315" src="http://www.youtube.com/embed/f_4MBuxMBN4&quot; frameborder="0" allowfullscreen></iframe>';
    }

    elseif($chosen_album== 3) {
    //west ryder.......
    $album_info = '<p class="album_title">West Ryder Pauper Lunatic Asylum</p>
    <p class="artist">Artist: Kasabian</p>

    <p class="track_list">Track List:</p>
    <ol class="list">
    <li>Underdog</li>
    <li>Where Did All The Love Go?</li>
    <li>Swarfiga</li>
    <li>Fast Fuse</li>
    <li>Take Aim</li>
    <li>Thick As Thieves</li>
    <li>West Ryder Silver Bullet</li>
    <li>Vlad The Impaler</li>
    <li>Ladies & Gentlemen (Roll The Dice)</li>
    <li>Secret Alphabets</li>
    <li>Fire</li>
    <li>Happiness</li>
    </ol>
    <iframe width="560" height="315" src="http://www.youtube.com/embed/tjMnhiCUnr0&quot; frameborder="0" allowfullscreen></iframe>';
    }
    ?>

    But when i hit "Submit" on the drop down menu, it just brings me to a blank PHP page. The URL of the .php is correct as well so there is obviously an error in my code, and nothing to do with the server host.


  • Registered Users Posts: 806 ✭✭✭Niall09


    You forgot to echo $album_info

    So under your last curly bracket put this in

    [PHP]echo $album_info;[/PHP]


  • Advertisement
  • Registered Users Posts: 2,589 ✭✭✭irish_stevo815


    Niall09 wrote: »
    You forgot to echo $album_info

    So under your last curly bracket put this in

    [PHP]echo $album_info;[/PHP]
    Thank you very much....this seemed to do the trick.

    Cheers for the help guys!!!!


Advertisement