出现验证消息后,下拉菜单将返回到“请选择”选项

时间:2012-11-23 00:21:36

标签: php html

下面我有一个表单,其中包含几个文本输入,一个下拉菜单,以及验证和成功的消息:

<?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;

?>

现在我遇到的问题实际上是下拉菜单。

如果显示成功消息,则会显示课程下拉菜单中的“请选择”选项,这很好,但如果用户提交表单并显示验证消息,则下拉菜单将返回到“请选择“选项。我希望它与用户选择的最后一个选项保持一致。如何通过上面的代码实现这一点?

1 个答案:

答案 0 :(得分:2)

更改以下行:

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

对此:

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;
}

这样做会检查当前$course是否等于$_POST['course']。如果是,则将选项设置为selected

修改1

您可以检查您的表单提交是否有效,以及稍后将使用以下内容传递该表单:

$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;
    }
}