【问题标题】:PHP and Ajax not working in Internet ExplorerPHP 和 Ajax 在 Internet Explorer 中不起作用
【发布时间】:2012-05-02 04:40:01
【问题描述】:

所以,我在我的网站上开发了一个实时搜索功能,但不足为奇的是,它似乎在 Internet Explorer 中不起作用。我已经测试过,它在 IE9 和 IE8 中不起作用,我知道它在 Chrome 和 Fire Fox 中可以正常工作。我使用的 Ajax 来自 W3Schools 网站,它似乎在那里工作。它甚至可能是我使用的 PHP,但我不确定问题是什么。我只是猜测它是 Ajax 还是 PHP。有谁知道怎么了?如果有帮助,它位于 -RETRACTED URL-。谢谢!

我的 HTML 文档中的 Ajax:

<script type="text/javascript">
var keyword = ''; // add this!
var city = ''; // add this!
var state = ''; // add this!
var country = ''; // add this!

function showHint(keyword, city, state, country) {
    var xmlhttp;
    if(keyword.length == 0 && city.length == 0 && state.length == 0) {
        document.getElementById("txtHint").innerHTML = "";
    }
    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("txtHint").innerHTML = xmlhttp.responseText;
        }
    }
    xmlhttp.open("GET", "gethint.php?keyword=" + keyword + "&city=" + city + "&state=" + state + "&country=" + country, true);
    xmlhttp.send();
}
</script>

用于实时搜索的 PHP:

<?php
//get the parameters from URL
$keyword = $_GET['keyword'];
$city = $_GET['city'];
$state = $_GET['state'];
$country = $_GET['country'];
$query = mysql_query("SELECT * FROM users WHERE ClinicName LIKE '%$keyword%' AND LocationCity LIKE '%$city%' AND LocationRegion LIKE '%$state%' AND LocationCountry LIKE '%$country%' ORDER BY ClinicName ASC") or die(mysql_error());

if($query){//If query successfull
    if(mysql_affected_rows()!=0){//and if atleast one record is found
        echo '
        <tr>
            <th>Clinic Name</th>
            <th>Phone Number</th>
            <th>Location</th>
        </tr>';
        while($rows = mysql_fetch_array($query)){ //Display the record
            $replace = str_replace(" ", "-", $rows['ClinicName']);
            echo '
            <tr>
                <td><a href="clinic.php?userid='.$rows['UserID'] .'">'.$rows['ClinicName'].'</a></td>
                <td>'.$rows['Phone1'].'-'.$rows['Phone2'].'-'.$rows['Phone3'].'</td>
                <td>'.$rows['LocationCity'].', '.$rows['LocationRegion'].' '.$rows['LocationZip'].', '.$rows['LocationCountry'].'</td>
            </tr>';
        }
    }
    else {
        echo 'No Results for:<br />Clinic Name: '.$keyword.'<br />City: '.$city.'<br />State: '.$state.'<br />Country: '.$country.'';//No Match found in the Database
    }
}
else {
    echo 'Parameter Missing in the URL';//If URL is invalid
}
?>

【问题讨论】:

  • 请先检查它是否适用于chrome或firefox
  • 是的,确实如此。很抱歉漏掉了。
  • 你亲自在 Chrome 中测试过吗?
  • 是的,我做到了。一切都如我所愿。
  • 为什么要忍受所有的痛苦,切换到 jQuery。

标签: php ajax internet-explorer


【解决方案1】:

IE不会让你innerHTML一个表格。您可以在此处找到此信息:Microsoft

【讨论】:

    【解决方案2】:

    试试这个:

    将变量 d 定义为日期: var d = new Date();

    然后添加到 GET

    }
    xmlhttp.open("GET", "gethint.php?keyword=" + keyword + "&city=" + city + "&state=" + state + "&country=" + country + "&q=" + d, true);     
    xmlhttp.send(); 
    } 
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2012-07-31
      • 1970-01-01
      • 2012-06-07
      • 2012-10-19
      • 1970-01-01
      • 1970-01-01
      • 2012-11-09
      相关资源
      最近更新 更多