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 852235
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:33:40+00:00 2026-05-15T07:33:40+00:00

I currently have a form built in which after validation, if errors exist, the

  • 0

I currently have a form built in which after validation, if errors exist, the data stays on screen for the consumer to correct. An example of how this works for say the ‘Year of Birth’ is:

<select name="DOB3">
    <option value="">Year</option>
    <?php
        for ($i=date('Y'); $i>=1900; $i--)
  {
    echo "<option value='$i'";
    if ($fields["DOB3"] == $i)
      echo " selected";
    echo ">$i</option>";
  }
  ?>
</select>

If an error is found, the year of birth value returns the year previously entered. I am able to have this work on all field with the exception of my ‘State’ field. I build the array and function for the drop down with the following code:

<?php

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

    function showOptionsDrop($array, $active, $echo=true){
        $string = '';

        foreach($array as $k => $v){
            $s = ($active == $k)? ' selected="selected"' : '';
            $string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";     
        }

        if($echo) {   echo $string;}
        else {       return $string;}
    }
?> 

I then call the function from within the form using:

<td><select name="State"><option value="">Choose a State</option><?php showOptionsDrop($states_arr, null, true); ?></select></td>

Not sure what I’m missing but would love any assistance if somebody sees the error in my code.

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-05-15T07:33:40+00:00Added an answer on May 15, 2026 at 7:33 am

    Have a look at the code:

    <?php showOptionsDrop($states_arr, null, true); ?>
    

    You are passing null, so $active will always be null. The condition

    ($active == $k)
    

    will never we evaluate to true.

    You should pass the value you get from the form instead, e.g.:

    <?php showOptionsDrop($states_arr, isset($fields['State']) ? $fields['State'] : null,  true); ?>
    

    You really should try to separate the PHP from the HTML, especially in your first example

    Update:

    Actually it is not that much, but consider to use the alternative syntax for control structures:

    <select name="DOB3">
        <optgroup label="Year">
        <?php for ($i=date('Y'); $i>=1900; $i--) : ?>
            <option value="<?php echo $i ?>" 
                 <?php echo ($fields["DOB3"] == $i) ? 'selected="selected"' : '' ?> > 
                 <?php echo $i ?> 
            </option>
        <?php endforeach; ?>
        </optgroup>
    </select>
    

    Also if you want to give the options some kind of label, you can do this with the optgroup HTML tag (this is not selectable).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 507k
  • Answers 507k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer No, they will not be removed, the dialog element itself… May 16, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer Maybe something like this: SELECT P.Title, C.Message, M.Username FROM Posts… May 16, 2026 at 3:54 pm
  • Editorial Team
    Editorial Team added an answer Never mind. I found the solution to this. The trick… May 16, 2026 at 3:54 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

Related Questions

I currently have a class of this form: class Abc { private readonly IDisposable
currently I have following code: home.php <form name='myformname' id='myformid'> <input type='text' name='mytext1' value='abc'> <input
I have an ASP.NET MVC server app and client code which uses jquery. I'd
I have a form where an admin will upload three pictures with different dimensions
I'm currently creating a very small form on my homepage using HTML and JavaScript.
Hey. We're building a large ASP.NET website, and have hired an external firm to
We've just had some Penetration Testing carried out on an application we've built using
I built a Winform app several months ago that schedules appointments for repair techs.
I have a parent WinForm that has a MyDataTable _dt as a member. The
I have a normal VS2008 project that I've been working on for awhile that

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.