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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T19:04:59+00:00 2026-06-14T19:04:59+00:00

below I have a form where it contains a couple of text inputs, a

  • 0

below I have a form where it contains a couple of text inputs, a drop down menu, and messages for validation and success:

<?php

$getyear = (isset($_POST['year'])) ? $_POST['year'] : '';

$sql = "SELECT CourseId, CourseName FROM Course ORDER BY CourseId";

$sqlstmt=$mysqli->prepare($sql);

$sqlstmt->execute();

$sqlstmt->bind_result($dbCourseId, $dbCourseName);

$courses = array(); // easier if you don't use generic names for data

$courseHTML = "";
$courseHTML .= '<select name="course" id="coursesDrop">'.PHP_EOL;
$courseHTML .= '<option value="">Please Select</option>'.PHP_EOL;

while($sqlstmt->fetch())
{
    $course = $dbCourseId;
    $coursename = $dbCourseName;
    $courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;

}

$courseHTML .= '</select>';

if((isset($_POST['registerbtn'])))
{
    $getfirstname = $_POST['firstname'];
    $getsurname = $_POST['surname'];
    $getcourse = $_POST['course'];


    if ($getfirstname)
    {
        if ($getsurname)
        {
            if ($getcourse)
            {
                // don't use $mysqli->prepare here
                $query = "SELECT StudentUsername FROM Student WHERE StudentUsername = ?";
                // prepare query
                $stmt=$mysqli->prepare($query);
                // You only need to call bind_param once
                $stmt->bind_param("s",$getid);
                // execute query
                $stmt->execute();
                // get result and assign variables (prefix with db)
                $stmt->bind_result($dbStudentUsername);
                //get number of rows
                $stmt->store_result();
                $numrows = $stmt->num_rows();


                if ($numrows == 1)
                {
                    $errormsg = "<span style='color: green'>Student has been Registered</span>";
                    $getfirstname = "";
                    $getsurname = "";
                    $getcourse = "";
                }
                else
                {
                    $errormsg = "An error has occured, Student has not been Registered";
                }
            }
            else
            {
                $errormsg = "You must select the Student's Course to Register";
            }
        }
        else
        {
            $errormsg = "You must enter in Student's Surname to Register";
        }

    }
    else
    {
        $errormsg = "You must enter in Student's First Name to Register";
    }
}

$form = "
<form action='" . htmlentities($_SERVER["PHP_SELF"]) . "' method='post'>
<table>
<tr>
<td></td>
<td id='errormsg'>$errormsg</td>
</tr>
<tr>
<td>First Name:</td>
<td><input type='text' name='firstname' value='$getfirstname' /></td>
</tr>
<tr>
<td>Surname:</td>
<td><input type='text' name='surname' value='$getsurname' /></td>
</tr>
<tr>
<td>Course:</td>
<td>{$courseHTML}</td>
</tr>
<tr>
<td></td>
<td><input type='submit' value='Register' name='registerbtn' /></td>
</tr>
</table>
</form>";

echo $form;

?>

Now the problem I am having is actually with the drop down menu.

If the succes message appears, then it displays the “Please Select” option for the course drop down menu which is fine, but if the user submits the form and a validation message appears, the drop down menu goes back to the “Please Select” option. I want it to stay with the last option the user has selected. How can this be achieved with the code above it is is possible?

  • 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-14T19:05:00+00:00Added an answer on June 14, 2026 at 7:05 pm

    Change the following line:

    $courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;
    

    To this:

    if (isset($_POST['course']) && $course == $_POST['course'])
    {
        $courseHTML .= "<option value='".$course."' selected='selected'>" . $course . " - " . $coursename . "</option>".PHP_EOL;
    }
    else
    {
        $courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;
    }
    

    What this does is checks if the current $course is equal to the $_POST['course']. If it is, it sets the option to selected.

    Edit 1

    You can check if your form submissions are valid, and that the form is going to pass later on with the following:

    $validSubmission = isset($_POST['registerbtn']) && $_POST['firstname'] && $_POST['surname'] && $_POST['course'];
    
    while($sqlstmt->fetch())
    {
        $course = $dbCourseId;
        $coursename = $dbCourseName;
        if (!$validSubmission && isset($_POST['course']) && $course == $_POST['course'])
        {
            $courseHTML .= "<option value='".$course."' selected='selected'>" . $course . " - " . $coursename . "</option>".PHP_EOL;
        }
        else
        {
            $courseHTML .= "<option value='".$course."'>" . $course . " - " . $coursename . "</option>".PHP_EOL;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a code below where it contains a form which contains text inputs
I have a form with 2 text inputs and 2 radio buttons, as below:
I have a div that contains a form with two drop down lists in
I have a variable that contains an array from form data, seen below: $option1
I have an asp.net content form which contains a couple of textboxes. I need
Form contains two dropdown lists created using code below. Both dropdown list elements have
I am using knockout.js and knockout.validation plugin. I have a search form which contains
I have a table in in which every row contains a form. see below
I have a form to signup with the code below: <form method=post> Username<input type=text
I have the form like below <form id=myform class=form-horizontal class=collapse in> <fieldset> <!-- form

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.