2014-02-24 76 views
0

得到本网站 - >http://www.secureshop.gr/POOL/acrosshotels/website/ 如果您在左侧栏检查是否有“查找酒店”侧栏,当您从下拉菜单中选择位置时,酒店菜单将更改选项。这适用于ajax。问题是它不适用于所有版本的IE。当您选择目的地时,酒店下拉菜单为空/空白。 JavaScript代码是这样的。非常简单和工程的目的地选项Ajax无法在IE上工作

<script type="text/javascript"> 

    function selecthotel(str) { 
    if (window.XMLHttpRequest) { 

     xmlhttp=new XMLHttpRequest(); 

    }else { 

     xmlhttp=new ActiveXObject("Microsoft.XMLHTTP"); 

    } 

    xmlhttp.onreadystatechange=function() { 
     if (xmlhttp.readyState==4 && xmlhttp.status==200) { 

     document.getElementById("hotelselection").innerHTML=xmlhttp.responseText; 

     } 
    } 

    if(str == 0) { 
     str = 0; 
    } 
    xmlhttp.open("GET","includes/ajaxlocationsearch.php?location="+str+"&language=<?php echo $language; ?>",true); 
    xmlhttp.send(); 

    } 

</script> 

阿贾克斯文件的onclick是这样的

$language = $_GET["language"]; 
    $location = $_GET['location']; 

if($location == "0") { 

    $result = mysql_query("Select * from eshop_articles where 
category='/WEBSITE/SEARCHENGINE/HOTELS' order by 
appearance",$link_id); 

}else { 

    $result = mysql_query("Select * from eshop_articles where 
category='/WEBSITE/SEARCHENGINE/HOTELS' and 
short_description='$location' order by appearance",$link_id); 

} ?> 

<option value="0"><?php $a = $language."_choose_hotel"; echo ${$a}; 
?></option> 

<?php while($row = mysql_fetch_assoc($result)) { ?> 

    <option value="<?php echo $row['appearance']; ?>"><?php echo 
$row['title']; ?></option> 

<?php } ?> 

预先感谢您:)

+0

为什么不使用$ .ajax()...? – Alex

+0

在IE 5中不支持jQuery Ajax,虽然这是一个很大的安全风险,但这很常见,这就是为什么使用XMLHttpRequest和ActiveXObject创建Ajax函数的类是必不可少的。 – ascx

+0

我有IE 11.仍然没有工作 – GeorgeGeorgitsis

回答

1

我做了一些测试,我发现,你的代码在结构上存在一些问题。您应该始终将代码格式正确,以便更快地找到错误和问题。我对代码进行了格式化,发现嵌套和查询存在一些问题。

我还想告诉你,你有一个非常严重的SQL注入问题,我通过使用预处理语句和一小段额外的preg_replace来解决查询和表中的所有不需要的字符。你应该完全去学习一些关于防止SQL注入的知识。但是也有一些专门讨论这个问题在这里巨大的话题,我做了这些物品的清单给你:

这里是我格式化和固定的代码。我已经通过使用无参数,空参数,数据库中不存在的值以及数据库中存在的值对其进行了测试。每一个返回相应的值:三个第一个返回null,而真正的查询返回true;在这种情况下,如果找不到,则返回“没有酒店可用”或者如果找到这些酒店的列表。如果数据库查询失败,它将默认返回null,然后返回“找不到酒店”。

我很抱歉改变代码布局一点点,随意编辑它,只要你喜欢,这取决于你。我强烈建议使用适当的格式(可能是因为您的代码编辑器)。

的index.php

<?php 
    $language = "en"; 
?> 

<!DOCTYPE html> 
<html> 
    <head> 
     <meta charset="utf-8" /> 
     <title>Hotel Selection</title> 
    </head> 
    <body> 
     <select id="hotelselection"> 
      <option value="null">No hotels available</option> 
     </select> 

     <script> 
      function selecthotel(str) { 
       if (window.XMLHttpRequest) { 
        xmlhttp = new XMLHttpRequest(); 
       }else{ 
        xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
       } 

       xmlhttp.onreadystatechange = function(){ 
        if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
         document.getElementById("hotelselection").innerHTML = xmlhttp.responseText; 
        } 
       } 

       if (typeof(str) == "undefined" || str == null) { 
        str = ""; 
       } 

       xmlhttp.open("GET", "run.php?location=" + str + "&language=<?php echo($language); ?>", true); 
       xmlhttp.send(); 
      } 

      selecthotel(); 
     </script> 
    </body> 
</html> 

run.php

<?php 
    $phrases = array(
     "en_error_db" => "No hotels available...", 
     "en_choose_hotel" => "Choose a hotel..." 
    ); 

    $link_id = mysqli_connect("localhost", "", "", ""); 

    if (mysqli_connect_errno($link_id)) { 
     die("Error occurred when attempting to connect to database (" . mysqli_connect_errno() . ": " . mysqli_connect_error() . ")."); 
     error_log("Error occurred when attempting to connect to database (" . mysqli_connect_errno() . ": " . mysqli_connect_error() . ")."); 
     exit(1); 
    } 

    $language_raw = isset($_GET["language"]) ? $_GET["language"] : "en"; 
    $location_raw = isset($_GET['location']) ? $_GET["location"] : ""; 

    $language = preg_replace("/[^\w.-]/", "", $language_raw); 
    $location = preg_replace("/[^\w.-]/", "", $location_raw); 

    if (empty($location)) { 
     $query = "SELECT * FROM `eshop_articles` WHERE `category` = '/WEBSITE/SEARCHENGINE/HOTELS' ORDER BY `appearance` ASC"; 
    }else{ 
     $query = "SELECT * FROM `eshop_articles` WHERE `category` = '/WEBSITE/SEARCHENGINE/HOTELS' AND `short_description` = ? ORDER BY `appearance` ASC"; 
    } 

    if ($stmt = mysqli_prepare($link_id, $query)) { 
     if (!empty($location)) { 
      mysqli_stmt_bind_param($stmt, "s", $location); 
     } 

     mysqli_stmt_execute($stmt); 

    // Thanks to Bruce Martin on php.net for the SELECT * via _fetch (http://www.php.net/manual/en/mysqli-stmt.fetch.php#107034) 
     $metaResults = mysqli_stmt_result_metadata($stmt); 
     $fields = mysqli_fetch_fields($metaResults); 
     $statementParams = ""; 

     foreach ($fields as $field) { 
      $statementParams .= (empty($statementParams) ? "\$column['" . $field->name . "']" : ", \$column['" . $field->name . "']"); 
     } 

     $statment = "\$stmt->bind_result($statementParams);"; 
     eval($statment); 
     print('<option value="0">' . $phrases[(isset($phrases[$language . "_choose_hotel"]) ? $language : "en") . "_choose_hotel"] . '</option>'); 

     while (mysqli_stmt_fetch($stmt)) { 
      print('<option value="' . $column['appearance'] . '">' . $column['title'] . '</option>'); 
     } 

     exit(1); 
    }else{ 
     print('<option value="0">' . $phrases[(isset($phrases[$language . "_choose_hotel"]) ? $language : "en") . "_error_db"] . '</option>'); 
     error_log("The script was unable to prepare a MySQLi statement (" . $query . ")."); 
     exit(1); 
    } 
?> 

我切换到MySQLi数据库扩展,而不是你deprecated MySQL extension。它不应该通过PHP错误日志返回PHP错误。如果可能,我强烈建议切换到MySQL PDO。这非常简单,容易,在我看来效果更好!

另外,关于您的XMLHttpRequest/ActiveXObject用法的说明:如果您希望能够支持IE 5,请为其创建一个类并在客户端使用该浏览器时加载该脚本,否则使用jQuery Ajax,这非常易于使用,你不需要担心查询字符串等。使用ActiveXObject脚本的原因是IE 5不支持jQuery,尽管存在已知的安全问题,但它仍然是一种常见的浏览器。 IE 5被旧计算机,一些银行,办公室和其他未查看安全细节的企业所使用。

希望这对你有所帮助。

+0

非常感谢你的时间,我会在几个小时内尝试它,但我认为它会工作,因为它看起来很好。再次感谢。 :) – GeorgeGeorgitsis

+0

不客气:-)当你接近它,让我知道如果它不起作用,我会试图弄清楚。 – ascx

+0

@satafaka:我建议现在重新复制代码。我继续前进,只是修改了代码以遵循准备好的语句:-) – ascx

1

Ajax的请求在Internet Explorer中的缓存。尝试删除缓存,然后随机参数添加到请求的URL:

var url = "http://example.com/ajax.php?random="+new Date().getTime(); 
+0

好吧,我这样做,你可以用萤火虫检查它。怎么办?还没有显示任何东西。 – GeorgeGeorgitsis

1

你不应该推倒重来,也有一些成熟的跨浏览器解决方案在那里了。

你应该尝试使用jQuery库,它的方法是ajax

https://api.jquery.com/jQuery.ajax/ 

如果你不想使用图书馆,你可以找到你的问题已经一些解决方法,它涉及到IE浏览器创建不同类型的对象:

http://www.quirksmode.org/js/xmlhttp.html 

InternetExplorer缓存的内容很多,因此您可能需要强制它获取新数据,而不是从缓存中获取数据。您可以添加一个GET参数,其中包含客户端生成的时间戳,指向您指向的URL。

在jQuery中你可以简单地做这样的:

jQuery.ajax({ 
    type: "GET", 
    url: "http://example.com/", 
    cache: false, 
    success: function (data) { 
     // do something here 
    } 
}); 

没有的jQuery,你需要手动将其添加到URL:

var url = "http://example.com" + "?_=" + (newDate()).getTime();