0

我的应用程序发生了一些奇怪的事情。下面是我的代码,其中用户在文本输入中输入 courseid 并提交文本输入。如果它从数据库中找到课程,则显示找到课程的回声,否则表明课程未找到。现在这适用于所有浏览器(IE、Opera、Safari、Firefox 和 Chrome)。下面是这个的代码:

<h1>CREATING A NEW 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>      <!-- Enter User Id here-->
</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 { 

echo "<p>Course Found: '$courseid'</p>";

}

但现在我决定更改设置,因此他们可以从下拉菜单中选择课程 ID,而不是在文本输入中输入 courseID。所以我把代码改成下面这样:

$sql = "SELECT CourseId, CourseName FROM Course"; 

$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">'.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>'; 
$courseHTML .= '</form>'; 

?>

<h1>CREATING A NEW ASSESSMENT</h1>

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">
<table>
<tr>
<th>Course: <?php echo $courseHTML; ?><input id="courseSubmit" type="submit" value="Submit" name="submit" /></th>
</tr>
</table>
</form>

<?php
if (isset($_POST['submit'])) {

$submittedCourseId = $_POST['courses'];

$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",$submittedCourseId);
// 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 style='color: red'>Please Select a Course</p>";
} else { 

echo "<p style='color: green'>Course Found '$courseid'</p>";

}

但这很奇怪,下拉菜单在 Chrome、Firefox 和 Safari 中有效,但在 Opera 和 IE 中无效。那么我的问题是,第二个代码块中有什么东西阻止它在 IE 或 Opera 中工作吗?

4

1 回答 1

0

删除$courseHTML .= '</form>';,它会工作。

事实是当您在表单中添加</form>$coureseHTML添加它时。表单结束标记完成表单。并且其余代码不属于表单。这里,<input id="courseSubmit" type="submit" value="Submit" name="submit" />不属于前面的形式。所以单击此按钮将不起作用。它不应该在任何浏览器中工作。可能是幸运地在 chrome、firefox 和 safari 上工作。

于 2012-10-27T13:46:20.290 回答