如何在表单提交后仍能选择下拉菜单值? [英] How can I get drop down menu value still selected after form is submitted?

查看:68
本文介绍了如何在表单提交后仍能选择下拉菜单值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我在文本框中输入数据并提交表单时,数据仍然存储在很好的文本框中。例如我使用的。我现在要做的是,当从下拉菜单中选择一个值并提交表单时,我希望所选择的值仍然显示为下拉列表中的选定值,但是我无法设法执行此操作。任何想法?

When I type in data in a text box and submit the form, the data is still stored in the text box which is great For example I used . What I want to do now is that when a value is selected from a drop down menu and the form is submitted, I want the value chosen to still be shown as the selected value on the drop down list but I cannot manage to do this. Any ideas?

以下是代码:

  <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<title>Exam Interface</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</head>
<body>

<?php

$username="xxx";
$password="xxx";
$database="mobile_app";

mysql_connect('localhost',$username,$password);

@mysql_select_db($database) or die("Unable to select database");



$sessionid = isset ($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset ($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset ($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset ($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset ($_POST['grade']) ? $_POST['grade'] : "";
$orderfield = isset ($_POST['order']) ? $_POST['order'] : "";

$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);

?>

<p><strong>NOTE: </strong>If a search box is left blank, then the form will search for all data under that specific field</p>

<form action="exam_interface.php" method="post" name="sessionform">        <!-- This will post the form to its own page"-->
<p>Session ID: <input type="text" name="sessionid" value="<?PHP print $sessionid ; ?>" /></p>      <!-- Enter Session Id here-->
<p>Module Number: <input type="text" name="moduleid" value="<?PHP print $moduleid ; ?>" /></p>      <!-- Enter Module Id here-->
<p>Teacher Username: <input type="text" name="teacherid" value="<?PHP print $teacherid ; ?>" /></p>      <!-- Enter Teacher here-->
<p>Student Username: <input type="text" name="studentid" value="<?PHP print $studentid ; ?>" /></p>      <!-- Enter User Id here-->
<p>Grade: <input type="text" name="grade" value="<?PHP print $grade ; ?>" /></p>      <!-- Enter Grade here-->
<p>Order Results By: <select name="order" value="<?PHP print $orderfield ; ?>">
<option value="ordersessionid">Session ID</option>
<option value="ordermoduleid">Module Number</option>
<option value="orderteacherid">Teacher Username</option>
<option value="orderstudentid">Student Username</option>
<option value="ordergrade">Grade</option>
</select>
<p><input type="submit" value="Submit" name="submit" /></p>
</form>

<?php

$username="u0867587";
$password="31may90";
$database="mobile_app";

mysql_connect('localhost',$username,$password);

@mysql_select_db($database) or die("Unable to select database");



$sessionid = isset ($_POST['sessionid']) ? $_POST['sessionid'] : "";
$moduleid = isset ($_POST['moduleid']) ? $_POST['moduleid'] : "";
$teacherid = isset ($_POST['teacherid']) ? $_POST['teacherid'] : "";
$studentid = isset ($_POST['studentid']) ? $_POST['studentid'] : "";
$grade = isset ($_POST['grade']) ? $_POST['grade'] : "";
$orderfield = isset ($_POST['order']) ? $_POST['order'] : "";

$sessionid = mysql_real_escape_string($sessionid);
$moduleid = mysql_real_escape_string($moduleid);
$teacherid = mysql_real_escape_string($teacherid);
$studentid = mysql_real_escape_string($studentid);
$grade = mysql_real_escape_string($grade);

if(isset($_POST['submit'])) {

switch ($orderfield) {
    case 'ordersessionid': $orderfield = 'gr.SessionId';
    break;
    case 'ordermoduleid': $orderfield = 'm.ModuleId'; 
    break;
    case 'orderteacherid': $orderfield = 's.TeacherId';
    break;
    case 'orderstudentid': $orderfield = 'gr.StudentId'; 
    break;
    case 'ordergrade': $orderfield = 'gr.Grade';
    break;
}

$result = mysql_query("SELECT * FROM Module m INNER JOIN Session s ON m.ModuleId = s.ModuleId JOIN Grade_Report gr ON s.SessionId = gr.SessionId JOIN Student st ON gr.StudentId = st.StudentId WHERE ('$sessionid' = '' OR gr.SessionId = '$sessionid') AND ('$moduleid' = '' OR m.ModuleId = '$moduleid') AND ('$teacherid' = '' OR s.TeacherId = '$teacherid') AND ('$studentid' = '' OR gr.StudentId = '$studentid') AND ('$grade' = '' OR gr.Grade = '$grade') ORDER BY $orderfield ASC");

$num=mysql_numrows($result);

echo "<p>Your Search: <strong>Session ID:</strong> "; if (empty($sessionid))echo "'All Sessions'"; else echo "'$sessionid'";echo ", <strong>Module ID:</strong> "; if (empty($moduleid))echo "'All Modules'"; else echo "'$moduleid'";echo ", <strong>Teacher Username:</strong> "; if (empty($teacherid))echo "'All Teachers'"; else echo "'$teacherid'";echo ", <strong>Student Username:</strong> "; if (empty($studentid))echo "'All Students'"; else echo "'$studentid'";echo ", <strong>Grade:</strong> "; if (empty($grade))echo "'All Grades'"; else echo "'$grade'"; echo ", <strong>Order Results By:</strong>";if ($orderfield == 'gr.SessionId') echo " 'Session ID'"; else if ($orderfield == 'm.ModuleId') echo " 'Module Number' "; else if ($orderfield == 's.TeacherId') echo " 'Teacher Username' "; else if ($orderfield == 'gr.StudentId') echo " 'Student Username' "; else if ($orderfield == 'gr.Grade') echo " 'Grade' ";"</p>";

echo "<p>Number of Records Shown in Result of the Search: <strong>$num</strong></p>";

echo "<table border='1'>
<tr>
<th>Student Id</th>
<th>Forename</th>
<th>Session Id</th>
<th>Grade</th>
<th>Mark</th>
<th>Module</th>
<th>Teacher</th>
</tr>";

while ($row = mysql_fetch_array($result)){

 echo "<tr>";
  echo "<td>" . $row['StudentId'] . "</td>";
  echo "<td>" . $row['Forename'] . "</td>";
  echo "<td>" . $row['SessionId'] . "</td>";
  echo "<td>" . $row['Grade'] . "</td>";
  echo "<td>" . $row['Mark'] . "</td>";
  echo "<td>" . $row['ModuleName'] . "</td>";
  echo "<td>" . $row['TeacherId'] . "</td>";
  echo "</tr>";
}

echo "</table>";

}

mysql_close();


 ?>

</body>
</html>

感谢您,任何帮助将对我有很大帮助。

Thank you and any help will be a big help for me.

推荐答案

这是我会做的。它不是富有魅力或完美,但它的工作非常好。如果表单提交的值匹配,您只需设置一个变量即可将选定的属性添加到选项标签中。然后只有当该变量与该选项匹配时才输出所选属性。这可能更难,但是这样做很简单。

Here is what I would do. It isn't glamourous or perfect, but it works amazingly well. You simply set a variable to add the selected attribute to your option tags IF the value coming from the form submit matches. Then output the selected attribute only if that variable matches that option. This can be way harder, but this works and is simple to understand.

为选择本身添加一个值对你来说并不会做任何事情。它必须添加到要自动选择的特定选项上。

Adding a value to the select itself doesn't do anything for you really. It has to be added on the particular option that you want to be auto-selected.

<?php if($orderfield=='ordersessionid'){$session = ' selected="selected"';} ?>
<?php if($orderfield=='ordermoduleid'){$module = ' selected="selected"';} ?>
<?php if($orderfield=='orderteacherid'){$teacher = ' selected="selected"';} ?>
<?php if($orderfield=='orderstudentid'){$student = ' selected="selected"';} ?>
<?php if($orderfield=='ordergrade'){$grade = ' selected="selected"';} ?>

<select name="order">
    <option value="ordersessionid" <?php echo $session; ?>>Session ID</option>
    <option value="ordermoduleid" <?php echo $module; ?>>Module Number</option>
    <option value="orderteacherid" <?php echo $teacher; ?>>Teacher Username</option>
    <option value="orderstudentid" <?php echo $student; ?>>Student Username</option>
    <option value="ordergrade" <?php echo $grade; ?>>Grade</option>
 </select>

这篇关于如何在表单提交后仍能选择下拉菜单值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆