代码适用于Chrome,Firefox和Safari,但不适用于IE或Opera

时间:2012-10-27 13:41:23

标签: php html browser pdo mysqli

我的应用程序发生了一些奇怪的事情。下面是我的代码,用户在其中键入一个文本输入并提交文本输入。如果它从数据库中找到该课程,则会显示找到该课程的回声,否则表示找不到课程。现在,这适用于所有浏览器(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>";

}

但是现在我决定更改设置,而不是在文本输入中输入courseID,他们可以从下拉菜单中选择课程ID。所以我将代码更改为以下内容:

$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中工作吗?

1 个答案:

答案 0 :(得分:0)

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

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