Vkiu Vkiu - 1 month ago 10
jQuery Question

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

This is HTML code:

<?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>


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:

12345
23456
56789


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

Fix your .txt file to look more like this:

12345,23456,56789

Don't add in line breaks like this:

12345, 23456, 56789

Comments