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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:13:39+00:00 2026-05-26T13:13:39+00:00

How do I retain form values upon submit for a loop? I’ve got something

  • 0

How do I retain form values upon submit for a loop?

I’ve got something like this:

<?php
    printf ("<optgroup label=\"CATEGORIES\">");

    foreach ($category as $key => $val) {
    printf ("<option value=\"$key\">$val</option>");
    }
    printf ("</optgroup>");
?>

When hitting submit on another part of my form, it resets the values in the select to the top one. I want it to keep the same value. How? For things like input boxes I have this that works:

<option value="5" name="results" <?php if($_GET["results"] == '5'){  ?> selected="selected" <?php } ?> >5</option>

This works because I’m not dynamically populating the dropdown from a database, so I don’t need a loop to loop through values and populate the select.

  • 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-26T13:13:40+00:00Added an answer on May 26, 2026 at 1:13 pm
    printf ("<optgroup label=\"CATEGORIES\">");
    
    foreach ($category as $key => $val) {
      $option = '<option value="' . $key . '"';
      if ($_GET['select_name'] == $key) {
        $option .= ' selected="selected"';
      }
      $option .= ">$val</option>";
    
      print $option;
    }
    printf ("</optgroup>");
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My dilemma is this: I have a PHP page with a form that includes
I've read that using session is one way to retain the values from PHP
<form method=get action=> <select name=name onchange='this.form.submit()'> <option value=a>a</option> <option value=b>b</option> </select> <select name=location onchange='this.form.submit()'>
I have a file upload form how can I retain this file when there
Executing this code: mainLyr = [[CALayer layer] retain]; [mainLyr setFrame:CGRectMake(0.0,0.0,23.0,23.0)]; in debugger, I found
I'm leading a project where we'll be recording metrics data. I'd like to retain
I have a form (that consists of 25+ fields) and the values for these
I'm sure this is a piece of cake but I'm really struggling with something
Is there anyway to submit a form but have it remain on the page?
I have a select options form fields setup like below: <select name=options[] multiple=multiple> <option

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.