Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8753855
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T13:30:30+00:00 2026-06-13T13:30:30+00:00

I found this code below and it works perfect for what I want BUT

  • 0

I found this code below and it works perfect for what I want BUT I have over 30 options is there something else I can do to shorten the code?

<html>
<body>
    <form method="post" action="?">
    <select name="dropdown">
    <option value="Jehzeel1">Jehzeel1</option>
    <option value="Jehzeel2">Jehzeel2</option>
    <option value="Jehzeel3">Jehzeel3</option>
    </select>
    <input type="submit" value="submit">
    </form>
</body>
</html>



    <?php
    switch ($_POST['dropdown'])  { 

        case "Jehzeel1":
         echo "Jehzeel likes apples";
        break;

        case "Jehzeel2":
         echo "Jehzeel likes bananas";
        break;

        case "Jehzeel3":
         echo "Jehzeel likes oranges";
        break;

    ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T13:30:32+00:00Added an answer on June 13, 2026 at 1:30 pm

    It’s easier to create a mapping array:

    $map = array(
        'Jehzeel2' => 'Jehzeel likes bananas';
        'Jehzeel3' => 'Jehzeel likes oranges';
    );
    
    echo $map[$_POST['dropdown']];
    

    Although you may want to think twice about your code structure, this looks like a bad practice.

    Short way:

    <?php
    $fruits = array('apples', 'oranges', 'bananas');
    ?>
    <form method="post">
        <select name="dropdown">
        <?php foreach ($fruits as $fruit) : ?>
            <option value="<?php echo $fruit ?>"><?php echo $fruit ?></option>
        <?php endforeach; ?>
        </select>
        <input type="submit" value="submit" />
    </form>
    <?php
    if (in_array($_POST['dropdown'], $fruits)) {
        echo 'Jehzeel likes ' . $_POST['dropdown'];
    }
    ?>
    

    EDIT

    You can use urls by slightly changing the array and the if statement:

    $urls = array('url1' => 'http://www.facebook.com/', 'url2' => 'http://www.google.com/', 'url3' => 'http://www.yahoo.com/');
    
    if (isset($urls[$_POST['dropdown']])) {
        echo 'URL: ' . $urls[$_POST['dropdown']];
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I found this orientation test code below looking for JQTouch reference material. This works
I have found this code to make php file cache and compress. Below is
I found this code for alphanumeric check (Letters, numbers, spaces or underscores) but I
I found this code from here: http://www.cssportal.com/form-elements/text-box.htm But the problem is you can still
I have something strange going on with my application. Below is my code where
I have been messing with this code for quite some time, works in FF,
I have found a code for checking whether string is numeric. And it works
I have this code below: /** * Lists all User entities. * * @Route("/{cid}",defaults={"cid"
I am running the code below in some if/else statements, I have a weird
I have the below query and it works perfect , no issues SELECT description

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.