Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial


Welby Obeng
Welby Obeng
20,340 Points

Return a form back with the values the user typed by echoing $_POST[""] in the value field.

I am working on a form and I have conditions the form have to pass. If there is an error. I return the form back with the values the user typed by echoing $_POST[""] in the value field for each input. I know how to do it if it;s a input method but not when it's a select dropdown..how will I do that in php

 <label for="fname">*First Name:</label>
          <input type="text" id="fname" name="user_fname" value="<?php echo trim($_POST["user_fname"]) ; ?>" required>

          <label for="lname">*Last Name:</label>
          <input type="text" id="lname" name="user_lname" value="<?php echo trim($_POST["user_lname"]) ; ?>" required>

          <label for="middle">Middle Name:</label>
          <input type="text" id="middle" name="user_mname" value="<?php echo trim($_POST["user_mname"]) ; ?>" >

          <label for="street">*Street Address:</label>
          <input type="text" id="street" name="street" value="<?php echo trim($_POST["street"]) ; ?>" required>

          <label for="city">*City:</label>
          <input type="text" id="city" name="city" value="<?php echo trim($_POST["city"]) ; ?>" required>

          <label for="state">*State:</label>
                <select name="state" required>
                <option value="AL">Alabama</option>
                <option value="AK">Alaska</option>
                <option value="AZ">Arizona</option>
                <option value="AR">Arkansas</option>
                <option value="CA">California</option>
                <option value="CO">Colorado</option>
                <option value="CT">Connecticut</option>
                <option value="DE">Delaware</option>
                <option value="DC">District Of Columbia</option>
                <option value="FL">Florida</option>
                <option value="GA">Georgia</option>
                <option value="HI">Hawaii</option>
                <option value="ID">Idaho</option>
                <option value="IL">Illinois</option>
                <option value="IN">Indiana</option>
                <option value="IA">Iowa</option>
                <option value="KS">Kansas</option>
                <option value="KY">Kentucky</option>
                <option value="LA">Louisiana</option>
                <option value="ME">Maine</option>
                <option value="MD">Maryland</option>
                <option value="MA">Massachusetts</option>
                <option value="MI">Michigan</option>
                <option value="MN">Minnesota</option>
                <option value="MS">Mississippi</option>
                <option value="MO">Missouri</option>
                <option value="MT">Montana</option>
                <option value="NE">Nebraska</option>
                <option value="NV">Nevada</option>
                <option value="NH">New Hampshire</option>
                <option value="NJ">New Jersey</option>
                <option value="NM">New Mexico</option>
                <option value="NY">New York</option>
                <option value="NC">North Carolina</option>
                <option value="ND">North Dakota</option>
                <option value="OH">Ohio</option>
                <option value="OK">Oklahoma</option>
                <option value="OR">Oregon</option>
                <option value="PA">Pennsylvania</option>
                <option value="RI">Rhode Island</option>
                <option value="SC">South Carolina</option>
                <option value="SD">South Dakota</option>
                <option value="TN">Tennessee</option>
                <option value="TX">Texas</option>
                <option value="UT">Utah</option>
                <option value="VT">Vermont</option>
                <option value="VA">Virginia</option>
                <option value="WA">Washington</option>
                <option value="WV">West Virginia</option>
                <option value="WI">Wisconsin</option>
                <option value="WY">Wyoming</option>

2 Answers

This depends how you create all the options in your select element.

If you had an array of all the states:


$states = array('state1', 'state2', 'state3');

you could foreach around the array to create your options

 <label for="state">*State:</label>
 <select name="state" required>


foreach ($states as $state) {
 echo '<option value="' . $state  . '">' . $state . '</option>';



you would then need to check if the $_POST['state'] is equal to the current state in the foreach loop, and if it is, give the option the class of selected.

 <label for="state">*State:</label>
 <select name="state" required>


foreach ($states as $state) {
 if ($state === $_POST['state']) {
  $selected = 'selected';
} else {
  $selected = '';
 echo '<option value="' . $state  . '" ' . $selected . '>' . $state . '</option>';



I notice you have different values and information inside your options elements.

You might need a two dimensional array:


$states = array(
'state1' => array('value' => 'S1', 'text' => 'State 1'),
'state2' => array('value' => 'S2', 'text' => 'State 2'),

Welby Obeng
Welby Obeng
20,340 Points

I create the options by manually pasting the available options

Adrian Zamfir
Adrian Zamfir
13,529 Points

Hy. You can check if the $_POST["state"] equals value than you echo selected on the option with that value.

if($_POST["state"] == "value") echo "selected";

Hope this helps!

Welby Obeng
Welby Obeng
20,340 Points

Let's say the user select value CA..how will I return that back?