How do I include a drop down list box in a PHP form?

I am updating a PHP form which has some form elements like radio buttons, check boxes and text boxes. I would like to include a drop down list box in the form which has the options 1 through 13 for the user to select but am not sure of the correct syntax.

For example, some of the code in the form is as follows:
.
.
.
<div class="field field-email">
                    <label for="emailAddress">E-Mail<span class="required">*</span></label>
                    <input name="emailAddress" id="emailAddress" type="email" value="" />          
                </div>

 <div class="field field-textarea">
                    <label for="howHear">How did you get to know about Apicius?</label>
                    <textarea name="howHear" id="howHear" cols="50" rows="3" style="width:95%"></textarea>            
                </div>
               
                <div class="field field-radios">
                    <label >Knowledge of the Italian Language?</label>
                    <div class="radioList">
                        <input name="italianLevel" type="radio" value="Beginner"  />&nbsp;Beginner
                        <input name="italianLevel" type="radio" value="Intermediate"  />&nbsp;Intermediate
                        <input name="italianLevel" type="radio" value="Advanced"  />&nbsp;Advanced
                </div>
.
.
.

There is also a separate "controller form" which submits the form entries as email parameters, e.g. the above entries would be represented as follows:
.
.
.

E-Mail: {$params['emailAddress']}

How did you hear about Apicius?  {$params['howHear']}

Knowledge of the Italian Language? {$params['italianLevel']}
.
.
.
geeta_m9Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Radek BaranowskiFull-stack Java DeveloperCommented:
the HTML element would be <select>:

<select name="formGender">
  <option value="">Select...</option>
  <option value="M">Male</option>
  <option value="F">Female</option>
</select>

Open in new window


and respective php to read selected value aftre submit button is used would be:

<?php
 
if(isset($_POST['formSubmit']) )
{
  $varGender = $_POST['formGender'];
  $errorMessage = "";
 
  // - - - snip - - - 
}?>

Open in new window


but that assumes you type in all options manually

if you use pure PHP (no framework) you can use some simple while or for loop to generate options, like:

<?php

echo "<select name=\"formFieldName\">
foreach ($option as $o) {
            echo "<option value=\"".$o."\">"    
echo "</select>
}

Open in new window


but in some (CodeIgniter is the one I know ) you can simply generate select element with a respective predefined function

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
elepilCommented:
The syntax for a drop down in HTML is:

<select>
    <option value="none">-Select State-</option>
    <option value="AL">Alabama</option>
    <option value="AK">Alaska</option>
    <option value="AZ">Arizona</option>
    <option value="AR">Arkansas</option>
    <option value="CA" selected="selected">California</option>
    <option value="CO">Colorado</option>
    <option value="CT">Connecticut</option>
</select>

Open in new window


Is that what you're asking?
geeta_m9Author Commented:
What about the "id" part. Don't I need that too?

Also when I pass the input as a parameter in the controller form, do I just write:

FormFieldName: {$params['formFieldName']}
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

elepilCommented:
Whether you use "id" or "name" depends on how you're processing your form. From your code, I'm seeing both, and I'm not sure why. In your code snippet, you indicated you were obtaining your form values by:

E-Mail: {$params['emailAddress']}

Open in new window


Since "id" and "name" both have identical values, I'm not sure which one you're picking up the 'emailAddress' value from.

Are you using some kind of PHP framework? Because I process my forms pretty much like Radek Baranowski does (the other responder). So I'm not sure how you got your form values into an array called $params.
geeta_m9Author Commented:
I am not sure. But since both the name and id are identical in this case, I guess it doesn't matter.
elepilCommented:
Geeta,

It sounds like you are using a framework, and I say that because you used the word "controller form". That word is never used when you're simply processing a HTML form using PHP scripts on the server side. So maybe your question pertains to the framework that you are using? It sounded like you were originally just asking about the syntax for a HTML dropdown, but it sounds like your question spans way beyond that.
geeta_m9Author Commented:
The form I am using is an external form used in Concrete 5.
Vimal DMSenior Software EngineerCommented:
Hi,

<form action="dump.php" method="POST" name="form">
<select name="selmenu" onChange="findselected()">
  <option value="1">Choice 1</option>
  <option value="<?php echo $wah;?>">Choice 2</option>
</select>
geeta_m9Author Commented:
Thanks.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.