2016-02-24 91 views
0

我有一個jQuery自動完成功能,由json_encoded PHP數組提供。一切正常,但當我右鍵點擊該網站查看頁面源時,我可以看到整個數組。這個數組包含哪些敏感信息?有沒有更好的方法來組織這些代碼,使它在相同級別的功能上更私密?有沒有辦法從HTML源代碼隱藏自動完成數組?

在主PHP/HTML頁面:

<?php include 'autocomplete.php'; ?> 

,其中包括:

<?php 

// connect to db 

    //fetch first and last name 
    $sql="SELECT first, last FROM names"; 
    $result = mysqli_query($web_dbi, $sql) or die("Error " . mysqli_error($web_dbi)); 
    while ($f=mysqli_fetch_array($result)) { 
     $names[] = array(
     'label' => $f['first'] . " " . $f['last']; 
     ); 
    } 

    echo json_encode($names); 

?> 

背面的主PHP/HTML頁面上,我有一些JQuery的:

... (JQuery CDNs) 
    <script type="text/javascript"> 
     $(function() { 
      $("#inputfield").autocomplete({ 
      source: "autocomplete.php", 
      minLength: 1, 
      select: function(event, ui) { 
       log(ui.item ? 
       "Selected: " + ui.item.value + " aka " + ui.item.id : 
       "Nothing selected, input was " + this.value); 
      } 
     }); 
    }); 
    </script> 

的主要問題是,在HTML頁面的源代碼中,這是可見的:

$(function() { 
     var autocompletevalues = ["name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name","name"]; 
     $("#inputfield").autocomplete({ 
      dataType: "json", 
      source: availableTags 
     }); 
    }); 
+0

自動完成值是通過AJAX動態牽強,你沒有將其存儲在一個變量。 –

+0

是的,我做到了。如果正確實施,您的HTML頁面源不應該以這種方式顯示自動完成值。 –

+0

所以你推薦我用ajax實現自動完成嗎? – draft

回答

0

它必須是這樣的:

$("#birds").autocomplete({ 
      source: "search.php", 
      minLength: 2, 
      select: function(event, ui) { 
       log(ui.item ? 
        "Selected: " + ui.item.value + " aka " + ui.item.id : 
        "Nothing selected, input was " + this.value); 
      } 
     }); 

價:JQuery Autocomplete

search.php具有立朗科技json值然後可以被顯示爲自動填充的一部分。

請參閱本小提琴:http://jsfiddle.net/handtrix/32Bck/

更新1:

while($product_search->fetch()) 
     { 
      $data[] = array(
       'label' => trim($product_code) 
      ); 
     } 

     echo json_encode($data); 

JS代碼:

$("#inputfield").autocomplete({ 
     source:'search.php', 
     minLength:4 
    }); 
+0

它沒有完成...: – draft

+0

xhr.send((s.hasContent && s.data)||空);在search.php文件中, – draft

+0

是「echo json_encode($ names);」正確? – draft

相關問題