搜索文本文件与用户输入PHP [英] Search Text Files with PHP from User Input

查看:118
本文介绍了搜索文本文件与用户输入PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

寻找一个解决方案,以通过邮编codeS的搜索列表。我有一帮拉链codeS,我们服务的一个文本文件。想有网站会要求用户输入他们的zip code上的表单,看看我们服务的区域。如果是的话,显示一条消息,说我们做的,如果不是,他说,我们不知道。思想的PHP将是我的问题,最好的解决办法,但我是一个总的小白,当谈到这一点。

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>

更新:preferably Ajax解决方案!

UPDATE: Preferably an AJAX solution!

推荐答案

(find_in_file_ajax.php)

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表单

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


原始的答案

测试

首先,文件需要通过的file_get_contents 访问,然后爆炸的每个条目并提取有关的拉链code搜索。


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.

假设ZIP codes.txt文件是格式如下:

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

43505
43517
43518
43526
43543

43505
43517
43518
43526
43543

注意:如果43505进行查询,就会发现。不同于4350和3505将不会被发现,所以这是一个唯一的查询。

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

考虑以下几点:

<?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";
}
?>

这篇关于搜索文本文件与用户输入PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆