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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T16:20:38+00:00 2026-06-06T16:20:38+00:00

I am working on some form coding in PHP, and I have everything working

  • 0

I am working on some form coding in PHP, and I have everything working pretty well when JavaScript is enabled. But from past experience and poor experiences with other sites not doing well when JavaScript is disabled, I thought I would pay more attention to it for this current project.

Regarding input type="text" POST fields, this is not a problem. But for the drop-down I happen to be using, I cannot get it to persist. Well, I can, but then the default selection is not correct. Here is some code…

Basic drop-down structure:

<select id="topic" name="topic">
    <option value="Select an option">Select an option</option>
    <option value="Topic A">Topic A</option>
    <option value="Topic B">Topic B</option>
    <option value="Topic C">Topic C</option>
</select>

I thought maybe if I blended selected with POST data, I could get it to work like this:

<select id="topic" name="topic">
    <option selected="selected" value="Select an option">Select an option</option>
    <option value=""><?php echo $_POST['topic']; ?></option>
    <option value="Topic A">Topic A</option>
    <option value="Topic B">Topic B</option>
    <option value="Topic C">Topic C</option>
</select>

But it doesn’t persist; it only goes back to the default, ‘Select an option’.

Trying this didn’t work either:

<select id="topic" name="topic">
    <option value="Select an option">Select an option</option>
    <option selected="selected" value=""><?php echo $_POST['topic']; ?></option>
    <option value="Topic A">Topic A</option>
    <option value="Topic B">Topic B</option>
    <option value="Topic C">Topic C</option>
</select>

It persists, but doesn’t have the correct default. Starts out blank (because POST is not entered yet).

I have done some Google searches and not really found anything very useful for this kind of thing, so I’m not sure where to go next. Any ideas/suggestions?

Thank you very much in advance!

  • 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-06T16:20:41+00:00Added an answer on June 6, 2026 at 4:20 pm

    I normally do something like the following:

    <select id="topic" name="topic">
    <option value="Select an option">Select an option</option>
    <?php
        $topics=Array("Topic A","Topic B","Topic C");
        foreach($topics as $topic){
            echo '<option value="'.$topic.'"'.($_POST['topic']==$topic?' selected="selected"':'').'>'.$topic.'</option>';
        }
    ?>
    </select>
    

    What this does is print out the select box just as you’ve described, but it adds a selected=”selected” attribute to the option that matches your $_POST variable.

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

Sidebar

Related Questions

I am working on some code, basically I have a form. On that form
I am working on zend. I have a form with some checkboxes. I want
I am working with Zend Framework. I have a form with some fields, all
I've been working with some form processing providers and they seem to have a
I am working on a php form with some jquery, especially for the error
I'm coding my first ever PHP contact form. I've been working on it all
I'm working on some form UI coding and wrote this jQuery one-liner: $('input[type=text]').val($(this).attr('default-value')); Intention
I have been working on a coding problem for some pages I inherited where
I am busy with some form validation and it is working but the messages
I am working on some simple form validation and need some assistance. Basically, I

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.