Seth Douwsma
Seth Douwsma

Reputation: 67

Search Text Files with PHP from User Input

Looking for a solution to search through a list of Zip Codes. I have a text file with a bunch of zip codes that we service. Would like to have a form on the website asking for the user to input their zip code to see if we service that area. If it does, display a message saying that we do, if not, saying that we don't. Thought PHP would be the best solution for my problem, but I'm a total noob when it comes to that.

I have my form set up, I'm just not sure how to search the text file and display the answer in another div?

<form action="zipcode.php" method="post">
<input type="text" name="search" />
<input type="submit" />
</form>

UPDATE: Preferably an AJAX solution!

Upvotes: 2

Views: 5074

Answers (2)

Funk Forty Niner
Funk Forty Niner

Reputation: 74219

AJAX method (tested)


PHP handler

(find_in_file_ajax.php)

<?php
$search = $_POST['search'];
$text = file_get_contents('zipcodes.txt');
$lines = explode("\n", $text);
if(in_array($_POST['search'], $lines)){ //checks if ZIP is in array
    echo "ZIP code found";
}else{
    echo "ZIP code does not exist";
}
?>

HTML form

<!DOCTYPE html>
<html>
<head>

<style>
.update {

font-family:Georgia;
color:#0000FF;

}
</style>

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js"></script>

<script type="text/javascript">

$(function() {

    $(".search_button").click(function() {
        // getting the value that user typed
        var searchString = $("#search_box").val();
        // forming the queryString
        var data = 'search='+ searchString;

        // if searchString is not empty
        if(searchString) {
            // ajax call
            $.ajax({
                type: "POST",
                url: "find_in_file_ajax.php",
                data: data,
                beforeSend: function(html) { // this happens before actual call
                    $("#results").html(''); 
                    $("#searchresults").show();
                    $(".word").html(searchString);
               },
               success: function(html){ // this happens after we get results
                    $("#results").show();
                    $("#results").append(html);
              }
            });    
        }
        return false;
    });
});
</script>

</head>

<body>
<div id="container">
<div>
<form method="post" action="">
    <input type="text" name="search" id="search_box" class='search_box'/>
    <input type="submit" value="Search" class="search_button" /><br />
</form>
</div>      
<div>

<div id="searchresults">Search results: <span id="results" class="update"></span>
</div>

</div>
</div>

</body>
</html>

Original answer

Tested

First the file needs to be accessed via file_get_contents, then explode each entry and extract the zip code search in question.

Assuming the zipcodes.txt file is in the following format:

43505
43517
43518
43526
43543

NOTE: If 43505 is queried, it will be found. Unlike 4350 or 3505 will not be found, so it's a unique query.

Consider the following:

<?php
$search = $_POST['search'];
$text = file_get_contents('zipcodes.txt');
$lines = explode("\n", $text);
if(in_array($_POST['search'], $lines)){ //checks if ZIP is in array
    echo "ZIP code found.";
}else{
    echo "ZIP code does not exist";
}
?>

Upvotes: 1

Alex O&#39;rourke
Alex O&#39;rourke

Reputation: 81

Saw your edit...the below is PHP.

I would do something like

$lines = file("/path/to/file.txt", FILE_IGNORE_NEW_LINES); //reads all values into array
if(in_array($_POST['search'], $lines)){ //checks if ZIP is in array
    echo "found zip code";
}else{
    echo "zip code does not exist";
}

As long as there isn't a VERY LARGE amount of zip codes...this should be fine. Also, what is the format of your file? This may not work.

Upvotes: 1

Related Questions