【问题标题】:Search Text Files with PHP from User Input使用 PHP 从用户输入中搜索文本文件
【发布时间】:2013-08-31 20:15:46
【问题描述】:

寻找搜索邮政编码列表的解决方案。我有一个包含我们服务的一堆邮政编码的文本文件。希望在网站上有一个表格,要求用户输入他们的邮政编码,以查看我们是否为该地区提供服务。如果是,则显示一条消息说我们这样做,如果不是,则显示我们不这样做。认为 PHP 将是解决我的问题的最佳解决方案,但在这方面我完全是菜鸟。

我已经设置了表单,但我不确定如何搜索文本文件并在另一个 div 中显示答案?

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

更新:最好是 AJAX 解决方案!

【问题讨论】:

  • 你想要 AJAX 还是只是一个普通的 POST?
  • AJAX 将是最好的解决方案!
  • @SethDouwsma 考虑在下面尝试我的答案,测试并使用您的邮政编码文件。
  • 你是最棒的!像魅力一样工作。有没有办法在不重新加载页面的情况下使用 AJAX 来做到这一点?

标签: php jquery ajax search


【解决方案1】:

AJAX 方法(已测试)


PHP 处理程序

(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访问文件,然后分解每个条目并提取有问题的邮政编码搜索。

假设 zipcodes.txt 文件格式如下:

43505
43517
43518
43526
43543

注意:如果查询43505,就会找到。不像 4350 或 3505 不会被找到,所以它是一个唯一的查询。

考虑以下几点:

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

【讨论】:

  • 你是最棒的!像魅力一样工作。有没有办法在不重新加载页面的情况下使用 AJAX 来做到这一点?
  • @SethDouwsma 我很高兴能帮上忙。至于 Ajax 方法,您可以轻松实现它,但我没有足够的 Ajax 经验,但我认为您需要做的就是找到一个 Ajax 示例,其中要调用的 POST 文件将是理论上,上面的代码,但我相信它会起作用。
  • @SethDouwsma 如果您觉得我已经回答了您的问题,请接受我的回答,并在箭头下方的左侧打勾,这样它就不会留在未回答的类别中,干杯。跨度>
  • 现在我已经看到它在现场工作,这更有意义。非常感谢弗雷德!
  • @SethDouwsma 你说得对,Ajax 很棒。我已经涉足到足以为您找到解决方案,非常欢迎您 Seth,很高兴我能提供帮助,干杯! (和平)
【解决方案2】:

看到你的编辑...下面是 PHP。

我会做类似的事情

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

只要没有大量的邮政编码......这应该没问题。另外,你的文件格式是什么?这可能行不通。

【讨论】:

  • 我有大约 350 个邮政编码可供搜索,将来可能会更多。我没有看到用户的项目在哪里用于搜索文本文件?
  • @SethDouwsma 给我们一个例子来说明你的文本文件是如何存储的。
  • 是的,请提供您的邮政编码文件示例。此外,用户数据可以通过$_POST['search'] 从 zipcode.php 访问。用那个代替上面的"ZIP_CODE"
  • 该格式适用于上面的代码。 350个拉链不是很多,所以应该没问题。从 zipcode.php(您提交表单的地方)使用上述代码。
  • 啊啊啊!我明白了,好吧,AJAX 解决方案怎么样,所以我不必重新加载页面?对此有任何想法!你们帮了大忙!
猜你喜欢
  • 2017-04-14
  • 2021-11-23
  • 1970-01-01
  • 2019-03-21
  • 2013-12-06
  • 1970-01-01
  • 1970-01-01
  • 2017-04-16
  • 2011-04-19
相关资源
最近更新 更多