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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T07:57:38+00:00 2026-05-13T07:57:38+00:00

I have a php page where users may enter data into fields inside a

  • 0

I have a php page where users may enter data into fields inside a form.
The form also has a picture upload utility.
This page is refreshed every time a new picture is uploaded, so to ‘remember’ the values the user ALREADY has filled in, I have this code:

<input type="text" value="<?php echo @$_POST['name'];?>">

This DOESN’T work for drop lists or radios…

I have one solution from a previous Q but that would mean I would have to create all drop lists again in PHP, when they are in HTML now, and it is ALOT of options in the drop lists.

Is there any other way?

Here is the first solution:

$color = $_POST["colors"];
$colors = array("red","green","blue");

<select name="colors">
<?php foreach ($colors as $option) { ?>
  <option<?php print ($option == $color) ? " selected" : ""; ?>>
    <?php print $option; ?>
  </option>
<?php } ?>
</select>

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-13T07:57:39+00:00Added an answer on May 13, 2026 at 7:57 am

    Sorry to say, but you have to output valid HTML no matter how much of a pain that is. Best suggestion is to wrap it in a helper function and include it. Or even better, go find a good form handling library and use it. This problem has been solved 1000’s of times.

    XSS CONCERN: Your example code has a number of security flaws. Let me illustrate a simple way to handle a form cleanly. The following code illustrates an error-proof way to handle:

    • Reading input
    • Validation
    • Handling errors, with nice messages
    • Re-populating the form
    • Etc…

    Please note the use of htmlspecialchars and ENT_QUOTES where needed. Also, the use of the cond ? val1 : val2 operator ensures that there are no E_STRICT warnings omitted, without the use of @ (which can be terrible for performance).

    <?php
    $FirstName = trim(isset($_POST['FirstName']) ? $_POST['FirstName'] : '');
    $LastName = trim(isset($_POST['LastName']) ? $_POST['LastName'] : '');
    $Gender = trim(isset($_POST['Gender']) ? $_POST['Gender'] : '');
    
    $Action = trim(isset($_POST['Action']) ? $_POST['Action'] : '');
    
    $Errors = array();
    
    switch($Action)
    {
    case 'Process':
      // validation code here
      if(empty($FirstName))
      $Errors[] = 'First Name is required.';
    
      if(empty($LastName))
      $Errors[] = 'Last Name is required.';
    
      if($Gender != 'Male' and $Gender != 'Female')
      $Errors[] = 'Gender is required.';
    
      if(count($Errors) > 0)
      $break;
    
      // save data or whatever here
    
      // Redirect to next page
      header('Location: nextpage.php');
      exit;
    }
    
    ?>
    <html>
    <head>
     ...
    </head>
    <body>
    
    <?php if(count($Errors) > 0) { ?>
       <div class="error">
          <?php foreach($Error as $e) { ?>
             <p><?php echo htmlspecialchars($e); ?></p>
          <?php } ?>
       </div>
    <?php } ?>
    
    <form method="post" action="<?php echo htmlspecialchars($_SERVER['REQUEST_URI'], ENT_QUOTES); ?>">
      <p>
      First Name:
      <input type="text" name="FirstName" value="<?php echo htmlspecialchars($FirstName, ENT_QUOTES); ?>" />
      </p>
    
      <p>
      Last Name:
      <input type="text" name="LastName" value="<?php echo htmlspecialchars($LastName, ENT_QUOTES); ?>" />
      </p>
    
      <p>
      Gender:
      <select name="Gender">
        <option value=""></option>
        <option value="Male" <?php if($Gender == 'Male') echo 'selected="selected"'; ?>>Male</option>
        <option value="Female" <?php if($Gender == 'Female') echo 'selected="selected"'; ?>>Female</option>
      </select>
      </p>
    
    </form>
    
    </body>
    </html>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 254k
  • Answers 254k
  • 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 It should be more like this: class Vuln(models.Model): pub_date =… May 13, 2026 at 10:14 am
  • Editorial Team
    Editorial Team added an answer Looking at the man page for syslog, I see the… May 13, 2026 at 10:14 am
  • Editorial Team
    Editorial Team added an answer java.nio.file is coming in Java 7. It's not in Java… May 13, 2026 at 10:14 am

Related Questions

I have a PHP site that uses a fairly common authentication scheme. The entire
I put username and password to a form of mine. The action starts up
I have a page which should show a form with checkboxes, post back if
I have a partly inherited web application in PHP and after poking around with
I have a table of items, and a table of itemkeywords. When a user

Trending Tags

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

Top Members

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.