在下面的代码中,我有 2 个下拉菜单。一个是“课程”下拉菜单,另一个是“模块”下拉菜单:
下面是代码:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$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="courses" id="coursesDrop" onchange="getModules();">'.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>';
$moduleHTML = "";
$moduleHTML .= '<select name="modules" id="modulesDrop">'.PHP_EOL;
$moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;
$moduleHTML .= '</select>';
include('noscript.php');
?>
<script type="text/javascript">
function getModules() {
var course = jQuery("#coursesDrop").val();
jQuery('#modulesDrop').empty();
jQuery('#modulesDrop').html('<option value="">Please Select</option>');
jQuery.ajax({
type: "post",
url: "module.php",
data: { course:course },
success: function(response){
jQuery('#modulesDrop').append(response);
}
});
}
</script>
<h1>EDIT AN ASSESSMENT'S DATE/START TIME</h1>
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit" name="moduleSubmit" /></p>
</form>
<?php
if (isset($_POST['moduleSubmit'])) {
$sessionquery = "
SELECT SessionId, SessionDate, SessionTime, ModuleId, TeacherId
FROM Session
WHERE
(ModuleId = ? AND TeacherId = ?)
ORDER BY SessionDate, SessionTime
";
$sessionqrystmt=$mysqli->prepare($sessionquery);
// You only need to call bind_param once
$sessionqrystmt->bind_param("si",$moduleId,$userid);
// get result and assign variables (prefix with db)
$sessionqrystmt->execute();
$sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId, $dbTeacherId);
$sessionqrystmt->store_result();
$sessionnum = $sessionqrystmt->num_rows();
if($sessionnum ==0){
echo "<p>Sorry, You have No Sessions under this Module</p>";
module.php 在其中显示“模块”下拉菜单:
<?php
// connect to the database
include('connect.php');
/* check connection */
if (mysqli_connect_errno()) {
printf("Connect failed: %s\n", mysqli_connect_error());
die();
}
$course = isset($_POST['course']) ? $_POST['course'] : '';
$sql = "
SELECT cm.CourseId, cm.ModuleId,
c.CourseName,
m.ModuleName
FROM Course c
INNER JOIN Course_Module cm ON c.CourseId = cm.CourseId
JOIN Module m ON cm.ModuleId = m.ModuleId
WHERE
(c.CourseId = ?)
ORDER BY c.CourseId, m.ModuleId
";
$sqlstmt=$mysqli->prepare($sql);
$sqlstmt->bind_param("s",$course);
$sqlstmt->execute();
$sqlstmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
$moduleHTML = "";
while($sqlstmt->fetch()) {
$moduleHTML .= "<option value='$dbModuleId'>" . $dbModuleId . " - " . $dbModuleName . "</option>".PHP_EOL;
}
echo $moduleHTML;
$sqlstmt->execute();
?>
现在,如果您查看代码的底部,会发生什么情况是它在用户提交“课程”和“模块”下拉菜单后执行查询。如果它没有找到与该模块相关的会话,那么它会回显"Sorry, You have No Sessions under this Module"
。
但是我的问题是,如果用户没有从下拉菜单中选择“课程”和“模块”,或者用户从“课程”下拉菜单中选择了课程,我不希望显示此回声但尚未从“模块”下拉菜单中选择模块。
下面是我想要的每种情况的回声:
- 如果用户没有选择课程和模块:
echo "Please Select a Course and Module";
- 如果用户选择了课程但没有选择模块:
echo "Please Select a Module";
- 如果用户选择了课程和模块但没有出现会话:
echo "Sorry, You have No Sessions under this Module";
目前上面的列表 3 有效,但我怎样才能让列表 1 和 2 只显示它们的回声而不显示其他回声?
更新:
验证下拉菜单的 Javascript 代码:
function validation() {
var isDataValid = true;
var moduleTextO = document.getElementById("coursesDrop");
var courseTextO = document.getElementById("modulesDrop");
var errModuleMsgO = document.getElementById("moduleAlert");
if (courseTextO.value == "" && moduleTextO.value == ""){
errModuleMsgO.innerHTML = "Please Select a Course and Module";
isDataValid = false;
} else if (courseTextO.value == ""){
errModuleMsgO.innerHTML = "Please Select a Course";
isDataValid = false;
}else if (moduleTextO.value == ""){
errModuleMsgO.innerHTML = "Please Select a Module";
isDataValid = false;
}else{
errModuleMsgO.innerHTML = "";
}
return isDataValid;
}
下面是HTML代码:
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?></th>
<th>Module: <?php echo $moduleHTML; ?></th>
</tr>
</table>
<p><input id="moduleSubmit" type="submit" value="Submit" name="moduleSubmit" /></p>
<div id="moduleAlert"></div>
</form>