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

  • Home
  • SEARCH
  • 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 8014327
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T19:54:03+00:00 2026-06-04T19:54:03+00:00

I am creating an array and then displaying the elements of it with a

  • 0

I am creating an array and then displaying the elements of it with a dropdown using php and html. The array is varying size, so I have a foreach statement to display it, the problem is I can’t figure out why the first value in the dropdown is always empty (I want the first value to actually be from my array). Here’s the code, any help is much appreciated!

<html>
  <form id="form1" name="form1" method="post" onsubmit="" onreset="" action="thing.php">
<?php

$data = array();
$data[4] = 20.4541;
$data[5] = 32.5631;
$data[6] = 41.9715;
$data[7] = "text";
$data[10] = 2231;
$bestValue = 5;

echo " " . $key;
echo "<select name='dropdown'>";
echo "<option value=''></option>";

if($_POST['dropdown'] !=0){
    foreach($data AS $key=>$value) {
        $sel = '';
        if($key==$_POST['dropdown'] ) {
            $sel = "selected";
        }

        echo "<option value='".$key."' ".$sel.">".$key."</option>"; 
    }}      
else{

    foreach($data AS $key=>$value) {
        $sel = '';
        if($key==$bestValue ) {
                $sel = "selected";
        }
        echo $value;

        echo "<option value='".$key."' ".$sel.">".$key."</option>";             
    }}      

echo "</select>";


foreach($data AS $key2=>$value){
    if($_POST['dropdown'] == $key2){
    echo "  " . $data[$key2] . " degrees";}}
?>

<script>
function submitForm(action)
{
    document.getElementById('form1').action = action;
    document.getElementById('form1').submit();
}
</script>
<center>
<input type="button" onclick="submitForm('thing.php')" value="Find Angle" />
</center>

</html>

As you can see the first value in the dropdown is empty. How do I remove this? Thanks.

  • 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-04T19:54:05+00:00Added an answer on June 4, 2026 at 7:54 pm

    The first option you are printing is an empty option.

     echo "<select name='dropdown'>";
     echo "<option value=''></option>";
    

    Remove the line

    echo "<option value=''></option>";
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php file that is creating a array of everything in my
I'm creating the array dynamically using this tool. http://174.121.67.116/~achadire/aware5/product_options.php?cid=6 The array output looks like
It is not necessary to specify array size when creating array, right? Then, why
I have got the problem with creating an array list in c#, Can you
what i want to do is that i am creating php array dynamic .i
I'm creating a javascript 2D array from an XML file which I then manipulate
I have an array with a 14 strings. I want to display each of
I'm loading my table from an XML feed in to an array then creating
I am creating an array to store in cookie using jquery.cookies.js Below is the
Is there a general best practice for creating somewhat complex HTML elements in jQuery?

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.