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

The Archive Base Latest Questions

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

I have a form that validates the information submitted within a form, method is

  • 0

I have a form that validates the information submitted within a form, method is POST and is submitted to another file “Check.php”, I am using sessions to remember what the POST data is, when ever there is a problem with the form I redirect it back to the form input page.

How do you get the post data to remember which OPTION was selected in the SELECT input?

CODE:

<select name="referred">
    <option value="facebook">facebook</option>
    <option value="youtube">youtube</option>
    <option value="reddit">reddit</option>
    <option value="google">google</option>
</select>

I’ve tried to do this but does not work:

<select name="referred">
    <option value="facebook"   
    <?php
        if (isset($_POST['referred']) && $_POST['referred'] == "Facebook") {
            print "selected=\'selected\'";
        } 
    ?>?> >facebook</option>

I’ve tried to do this but still, does not work 🙁

<select name="referred">
    <option value="facebook"
    <?php
        if (isset($_POST['referred'])) {
            if ($_POST['referred'] == "youtube") { 
                echo "selected=\'selected\'";
            } else {
                echo "";
            }
        }
    ?> >facebook

Please help!

EDIT:

I APOLOGISE, I’m not using the $_POST here to check if it’s set, I should be using the $_SESSION, see below:

            <select name="referred">
                <option name="google"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "google"){ echo "selected='selected'";}else{ echo "";}}?>>google</option>
                <option name="youtube"<?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "youtube"){ echo "selected='selected'";}else{ echo "";}}?>>youtube</option>
                <option name="reddit" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "reddit"){ echo "selected='selected'";}else{ echo "";}}?>>reddit</option>
                <option name="facebook" <?php if(isset($_SESSION['referred'])){ if($_SESSION['referred'] == "facebook"){ echo "selected='selected'";}else{ echo "";}}?>>facebook</option>
            </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-27T07:40:49+00:00Added an answer on May 27, 2026 at 7:40 am

    You don’t need to escape single quotes in a string wrapped in double quotes.

    echo "selected=\'selected\'" // incorrect
    

    Like that your html markup would end up like <option value="facebook" selected=\'selected'\> Which is bad html.

    This should work.

    EDIT

    <select name="referred">
        <?php
            if (!empty($_SESSION['referred']) && 'option1' == $_SESSION['referred']) {
                $selected = 'selected="selected"';
            } else {
                $selected = null;
            }
        ?>
        <option value="option1" <?= $selected; ?>>option1</option>
        ....
    </select>
    

    If you have a known set of options then make an array and loop through them:

    <?php
        $options = array(
            'option1' => 'Option 1',
            'option2' => 'Option 2',
            'option3' => 'Option 3' // ...
        );
        $referredIsset = isset($_SESSION['referred']);
    ?>
    
    <select name="referred">
        <?php foreach ($options as $value => $name): ?>
            <?php
                $sel = ($referredIsset && $_SESSION['referred'] == $value)
                    ? 'selected="selected"'
                    : null;
            ?>
            <option value="<?= $value; ?>" <?= $sel; ?>>
                <?= $name; ?>
            </option>
        <?php endforeach; ?>
    </select>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple html form that submits information with POST function. But when
I have a form that validates using jquery. The form's id is contact and
I have a form that validates the email address and I want to be
I have a rails model that validates uniqueness of 2 form values. If these
this is a simple question. I have a form that is being validated using
The basics: I have a contact form that uses php to validate the forms.
I have a multipart form that takes basic user information at the beginning with
I have a form that is broken down into three different categories: Information, Violations,
MVC 3, VB.NET. I have a form in my app that gets basic information
I have a callback function that I'm using to validate a submitted video URL

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.