PHP、AJAX、MYSQLI 和 IE9 未按预期输出

标签 php jquery mysql ajax mysqli

我使用 PHP、AJAX 和 MYSQLI 的组合来加载用户输入内容的建议(每次按键时触发加载)。这一切在 chrome、ie10、firefox、safari 等中都运行得很好。然后我在 IE9 上尝试了它,它只是显示“无建议”(如果没有找到匹配项,我会编写代码来执行此操作)。我唯一的想法是,这可能是我在谷歌上读到的缓存问题,但是在尝试所有建议后..我最终回到了开始的地方..“没有建议”。所以,我很困惑。这是我的 AJAX 代码:

function activelyLoad(passedInfo, insertInto, urlInLib)
{
    if (passedInfo.length==0)
  { 
  document.getElementById(insertInto).innerHTML="";
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById(insertInto).innerHTML=xmlhttp.responseText;
    }
  }
xmlhttp.open("GET","../lib/"+urlInLib+"?q="+passedInfo,true);
xmlhttp.send();
}

这是我正在加载的 PHP 文件之一:

<?php
include('config.inc.php');
header( 'Cache-Control: no-store, no-cache, must-revalidate' );
header( 'Pragma: no-cache' );
$full = $_GET['q'];
$fullAr = explode(" ", $full);
$count = count($fullAr);
$var = $fullAr[0];
$query = "SELECT * FROM StudentInfo WHERE (StudentFirstName LIKE '%$var%' OR StudentPrefferedName LIKE '%$var%' OR StudentLastName LIKE '%$var%')";
$response = "";
if ($count >= 2) {
    $var2 = $fullAr[1];
    $query = "SELECT * FROM StudentInfo WHERE ((StudentFirstName LIKE '%$var%' OR StudentPrefferedName LIKE '%$var%') AND StudentLastName LIKE '%$var2%') OR ((StudentFirstName LIKE '%$var2%' OR StudentPrefferedName LIKE '%$var2%') AND StudentLastName LIKE '%$var%')";
}
$result = mysqli_query($connection, $query);
$num_results = mysqli_num_rows($result);
if ($num_results > 0) {
while ($row = mysqli_fetch_array($result)) {
    $id = $row['StudentID'];
    $functionList = "\"activelyLoad($id, 'detDiv', 'addtable.php'),showElem('#details')\"";
    $functionList2 = "\"logLoad($id),  showElem('#contactDets')\"";
    if(strcmp($row['StudentPrefferedName'], '') != 0){
    $response .= ("<tr><td>" . $row['StudentFirstName'] . " \"" . $row['StudentPrefferedName'] . "\" " . $row['StudentLastName'] . "</td><td><button type='button' onmousedown=" . $functionList . " onclick=" . $functionList2 . ">Get details</button></td></tr>");
    }
    else{
        $response .= ("<tr><td>" . $row['StudentFirstName'] . " " . $row['StudentLastName'] . "</td><td><button type='button' onmousedown=" . $functionList . " onclick=" . $functionList2 . ">Get details</button></td></tr>");
    }
}
}
else $response = "No suggestions";
echo($response);
mysqli_close($connection);
?>

以下是该页面在 IE9 中的相关 HTML 输出:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<META HTTP-EQUIV=”Pragma” CONTENT=”no-cache”>
<title>XXXXXXX</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.1/jquery.min.js"></script>
<script src="../lib/searchScripts.js"></script>
</head>
<body>
<h1>XXXXXXXX</h1>

<p>Please select one of the following options:</p>
<ul>
<li><a id="searchLink" href="#" onclick="showElem('#search')">Search for a student</a></li>

<div id="search">
<p><b>To find a student, start typing a name in the input field below:</b></p>
<form> 
Student Name: <input type="text" onkeyup="activelyLoad(this.value, 'txtHint', 'gethint.php'), showElem('#suggestions')">
</form>
<div id="suggestions"><h2>Suggestions:</h2> <div id="suggestionsBox"><table id="txtHint"><tr><td>No Suggestions</td></tr></table></div></div>

唯一让我认为这一定是缓存问题的是,这适用于所有现代的最新浏览器。非常感谢任何帮助。

编辑 IE9 的控制台返回

SCRIPT600: Invalid target element for this operation. for 'document.getElementById(insertInto).innerHTML=xmlhttp.responseText;'

这是我的问题吗?怎么无效呢?如果此处出现错误,浏览器如何能够访问 AJAX 来加载“无建议”?

最佳答案

在许多其他问题中,您试图将 AJAX 结果转储到 <table> 中元素为.innerHTML属性。

Internet Explorer 有一个错误,不允许您设置 table 的内容标签通过 .innerHTML直到 IE10 中修复该错误

错误报告:http://webbugtrack.blogspot.ca/2007/12/bug-210-no-innerhtml-support-on-tables.html

您最好创建一个 <div>并设置.innerHTML相反。

关于PHP、AJAX、MYSQLI 和 IE9 未按预期输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17576741/

相关文章:

php - Etsy PHP API - 交付选项

javascript - 按其中一个元素的值对动态生成的列表进行排序

mysql - wordpress搜索表单sql查询

Javascript 输入显示奇怪的数据

php - Facebook 应用程序开发 - 哪个选项,我可以使用 jQuery 吗?

php - 下拉菜单在 drupal 中不起作用

php - 从 MYSQL 中的每组中选择 N 行

javascript - 将文本写入搜索框后自动触发 Enter 键按下?

Jquery移动和PhoneGap : Reload page to get next item in database

mysql 无法启动 Ubuntu 16.04.2 x64