Vkiu Vkiu - 1 year ago 93
jQuery Question

Read .txt file in array and check if variable input exist in array

This is HTML code:

$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">

Thi is jQuery code:

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');


This is type of some variable in .txt file:


What i need: when i fill data in input field i can check its in variable in .txt file or not

Its working with last variable in array, not at all. I dont know where's the code is wrong ? Pls help.
Thanks in advance !

Answer Source

Fix your .txt file to look more like this:


Don't add in line breaks like this:

12345, 23456, 56789

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download