在我的代碼中,我有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 nd 2的工作只顯示他們的回聲,沒有其他的回聲?
UPDATE:
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>
什麼是源代碼'$ moduleId'?大概它應該來自'$ _POST' ['modules']',但我們沒有看到那個代碼。 –
@MichaelBerkowski模塊下拉菜單位於一個稱爲module.php的獨立頁面上,這是通過jquery進行的。代碼爲這我現在包括在問題 – user1723710
您是否在某處調用'驗證'功能? – mali303