提交后如何在页面上保持回显和下拉菜单

时间:2012-10-19 18:51:34

标签: php mysqli

在我的主应用程序中,我希望显示以下布局:

  1. 当应用程序打开时,它将显示“课程”文本输入和提交课程的提交按钮。 (这很好用)

  2. 当用户进入课程并提交时,如果成功,则会在回显中显示下方课程的名称和ID,并且在回声下方应显示“模块”下拉菜单并提交按钮提交模块。 (这很好)

  3. 当用户提交模块时,应执行以下操作:

  4. 应该保留课程名称的回声(此时用户提交模块

    后消失

    提交模块后,模块下拉菜单应保留在页面上(此时此消失

    从下拉菜单提交模块时,如何才能获得上述要点。

    <?php
    
         // connect to the database
     include('connect.php');
    
     /* check connection */
     if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    die();
    }
    
     $courseid = (isset($_POST['courseid'])) ? $_POST['courseid'] : '';
    
    ?>
    
       <h1>DELETING AN ASSESSMENT</h1>
    
        <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
         <p>Course ID: <input type="text" name="courseid" /><input id="courseSubmit" type="submit" value="Submit" name="submit" /></p>      
        </form>
    
    
        <?php
      if (isset($_POST['submit'])) {
    
        $query = "
                 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.CourseName, m.ModuleId
                ";
    
    $qrystmt=$mysqli->prepare($query);
    // You only need to call bind_param once
    $qrystmt->bind_param("s",$courseid);
    // get result and assign variables (prefix with db)
    
    $qrystmt->execute(); 
    
    $qrystmt->bind_result($dbCourseId,$dbModuleId,$dbCourseName,$dbModuleName);
    
     $qrystmt->store_result();
    
    $num = $qrystmt->num_rows();
    
    if($num ==0){
        echo "<p>Sorry, No Course was found with this Course ID '$courseid'</p>";
    } else { 
    
        $dataArray = array();
    
     while ( $qrystmt->fetch() ) { 
          // data array
          $dataArray[$dbCourseId]['CourseName'] = $dbCourseName; 
          $dataArray[$dbCourseId]['Modules'][$dbModuleId]['ModuleName'] = $dbModuleName; 
           // session data
          $_SESSION['idcourse'] = $dbCourseId;
          $_SESSION['namecourse'] = $dbCourseName;
    
    }
    
     foreach ($dataArray as $courseId => $courseData) {
    
          $output = ""; 
    
          $output .= "<p><strong>Course:</strong> " . $courseId .  " - "  . $courseData['CourseName'] . "</p>";
    
       $moduleHTML = ""; 
       $moduleHTML .= '<select name="modulesDrop" id="modulesDrop">'.PHP_EOL;
       $moduleHTML .= '<option value="">Please Select</option>'.PHP_EOL;      
            foreach ($courseData['Modules'] as $moduleId => $moduleData) {        
    
            $moduleHTML .= "<option value='$moduleId'>" . $moduleId . " - " .   
        $moduleData['ModuleName'] ."</option>".PHP_EOL;        
      } 
            }
            $moduleHTML .= '</select>';
    
      echo $output;
    
    
        ?>
    
         <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
         <p>Module: <?php echo $moduleHTML; ?><input id="moduleSubmit" type="submit" value="Submit" name="modulesubmit" /></p>      
        </form> 
    
    
    
        <?php
    }
    }
    
     if (isset($_POST['modulesubmit'])) {
    
     var_dump($_POST['modulesDrop']);
    
            $sessionquery = "
                 SELECT SessionId, SessionDate, SessionTime, ModuleId
                 FROM Session
                 WHERE
                 (ModuleId = ?)
                 ORDER BY SessionDate, SessionTime 
                ";
    
    $sessionqrystmt=$mysqli->prepare($sessionquery);
    // You only need to call bind_param once
    $sessionqrystmt->bind_param("s",$_POST['modulesDrop']);
    // get result and assign variables (prefix with db)
    
    $sessionqrystmt->execute(); 
    
    $sessionqrystmt->bind_result($dbSessionId,$dbSessionDate,$dbSessionTime, $dbModuleId);
    
     $sessionqrystmt->store_result();
    
    $sessionnum = $sessionqrystmt->num_rows();   
    
        $dataArraySession = array();
    
     while ( $sessionqrystmt->fetch() ) { 
    
     $dataArraySession[$dbSessionId]['SessionDate'] = $dbSessionDate; 
     $dataArraySession[$dbSessionId]['SessionTime'] = $dbSessionTime;
    
    }
    
    foreach ($dataArraySession as $sessionId => $sessionData) {
    
    
    
       $sessionHTML = ""; 
       $sessionHTML .= '<select name="session" id="sessionsDrop">'.PHP_EOL;
       $sessionHTML .= '<option value="">Please Select</option>'.PHP_EOL;           
       $sessionHTML .= "<option value='$sessionId'>" . $sessionId . " - " . $sessionData['SessionDate']. " - " . $sessionData['SessionTime'] ."</option>".PHP_EOL;        
       $sessionHTML .= '</select>';
    
            }
    
    
     if ($sessionnum > 0) {
    
     ?>
    
         <form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
         <p>Sessions: <?php echo $sessionHTML; ?><input id="sessionSubmit" type="submit" value="Submit" name="sesionsubmit" /></p>      
        </form>       
    
    <?php
    
    }
    
    else {
     echo "<p>Sorry, You have No Sessions under this Module</p>";
    }
    
    
    }
    
    
        ?>
    

1 个答案:

答案 0 :(得分:0)

您的代码有几个问题。您遇到的问题是您没有将第1阶段的信息“$ courseid”传递到第3阶段。为此,只需将以下内容添加到第2阶段

<input type="hidden" name="courseid" value="<?=$courseid?>" />