2016-10-13 69 views
0

存在这是我的HTML代码:阅读阵列txt文件,并检查变量输入数组

<?php 
     $filename = 'codes.txt'; 
     $data = file($filename); 
     foreach($data as $key => $val){ 
      $array[] = $val; 
     } 
?> 
<form id="checkinput" action="#"> 
<input class="zipcodes" name="zipcodes" value="" type="text"> 
<input class="zip-sub" type="button" value="Continue" id="submit"> 
</form> 

氏是jQuery代码:

jQuery(document).ready(function() { 
    jQuery('.zip-sub').click(function() { 
     var zip = jQuery('.zipcodes').val(); 
     var zipcodelist = <?php echo json_encode($array); ?>; 
     if(jQuery.inArray(zip, zipcodelist) != -1){ 
      alert('value is .txt file !'); 
     } else { 
      alert('value is not .txt file'); 
     } 
    }); 

}); 

这在某些变量的类型。 txt文件:

12345 
23456 
56789 

我需要的:当我填写输入字段我可以CHEC数据k其变量是否在.txt文件中

它与数组中的最后一个变量一起工作,根本没有。我不知道代码错在哪里?请帮助。 在此先感谢!

+0

我不是什么应该清楚在这里发生? – jonmrich

+0

只是编辑我需要的东西。谢谢 ! – OhIWill

+0

你能解释一下这是什么意思:“它与数组中的最后一个变量一起工作,根本就没有。” – jonmrich

回答

0

修复您的.txt文件看起来更像这样:

12345,23456,56789

不要行添加打破这样的:

12345, 23456, 56789

+0

这不会使用我的代码(即使在.txt文件中的最后一个变量)@@ – OhIWill

+0

很高兴为您工作。我总是发现,当某些东西无法正常工作似乎没有合乎逻辑的原因时,这是因为某处存在变量的问题 – jonmrich