在下面的代码中,我有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,显示“Module”下拉菜单:
<?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>
答案 0 :(得分:1)
你可以使用javascript在其中创建一个函数,在那里你可以检查天气每个字段是否填充可以在这里显示
<script type="text/javascript">
function validate()
{
if(document.dropdownmenuname.course.value=="")
{
alert("Please fill in your form");
document.dropdownmenuname.course.focus() ;
return false;
}
}
....
答案 1 :(得分:0)
更改表单标记
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
到
<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post" onsubmit="return validation();">
在服务器端进行验证也是一个好主意,如果要在服务器端检查值,只需在现有PHP中添加一些代码:
<?php
if (isset($_POST['moduleSubmit'])) {
if(empty($_POST['courses']) && empty($_POST['modules'])) {
echo 'Please Select a Course and Module';
} elseif(empty($_POST['courses'])) {
echo 'Please Select a Course';
} elseif(empty($_POST['modules'])) {
echo 'Please Select a module';
} else {
$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>";
}