2014-02-24 80 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();