2012-10-27 52 views
1

在我的代碼中,我有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"

但是我的問題是,如果用戶沒有從下拉菜單中選擇「課程」和「模塊」,或者用戶從「課程」中選擇了課程,我不希望顯示此回顯「下拉菜單,但沒有從」模塊「下拉菜單中選擇一個模塊。

下面是回聲的,我想對每一種情況:

  1. 如果用戶沒有選擇的課程和模塊:echo "Please Select a Course and Module";
  2. 如果用戶選擇了一個課程,但不是一個模塊:echo "Please Select a Module";
  3. 如果用戶選擇了一個課程和模塊,但沒有會話出現: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> 
+0

什麼是源代碼'$ moduleId'?大概它應該來自'$ _POST' ['modules']',但我們沒有看到那個代碼。 –

+0

@MichaelBerkowski模塊下拉菜單位於一個稱爲module.php的獨立頁面上,這是通過jquery進行的。代碼爲這我現在包括在問題 – user1723710

+0

您是否在某處調用'驗證'功能? – mali303

回答

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

} 
+0

謝謝,我可以問一個問題我可以問一個問題,如果我有單選按鈕,我可以使用php'empty'來檢查服務器端是否沒有選中單選按鈕嗎? – user1723710

+0

是的,無線電可以在服務器端進行驗證。 – mali303

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; 
} 
} 
.... 
+0

我已經在javascript代碼中包含了這樣的內容,但不是顯示警報,而是顯示一個字符串。問題是我無法獲取要顯示的字符串。我在我的問題中包含了一個更新,下面顯示了JavaScript代碼和HTML,你可以看到我的代碼有什麼問題,爲什麼這不起作用? – user1723710

+0

Upvote回答,謝謝 – user1723710

相關問題