【问题标题】:PHP Display search result text file in external html filePHP 在外部 html 文件中显示搜索结果文本文件
【发布时间】:2015-08-24 08:11:11
【问题描述】:

我正在尝试在文件 index.html 中创建一个 html 表单,从中我可以通过文件 searchFile.php 通过 PHP 查询文本文件中的一行字符,获取特定行并以所述形式显示在 index.html 中。

我的问题是我的 搜索结果 字段中没有显示任何内容,既不是错误消息也不是搜索结果。

更新!我已经进一步调查了我的问题,我意识到我使用 Search 按钮的方式不允许任何回声在页面之前保持显示在页面上再次更新,echocontent 被删除。不过,如果不使用两个按钮,一个用于搜索,一个用于获取搜索结果,我不知道该如何解决这个问题。

这是我目前想出的:

index.html

      </head>

                <body>
            <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.2/jquery.min.js">
           $(function() {

           // getting the search term
        var searchString = $("#search_box").val();
        // forming the searchString
        var data = 'search='+ searchString;

        //if searchString is not empty
        if(searchString) {
            // call ajax
            $.ajax({
                type: "POST",
                url: "searchFile.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>


<form name="theForm" method="post">
Name: <input type='text' onclick="ajaxFunction();" name='search' class='search_box' id='search_box'/> <br />
<input type='submit' value='Search' class="search_button" ><br />

</form>

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




</body>
</html>

searchFile.php

<?php
$search = explode(' ', $_REQUEST["search"]);

$lines = file('file-export.txt'); 
foreach($lines as $line) 
{ 
    if(strpos($line, $search[0]) !== false && strpos($line, $search[1] !== false)) { 
        echo '<div id="searchresults">' . $search . '</div>';
    }else{

    echo '<div id="searchresults">' . "No match" . '</div>';


    }
} 
?>   

编辑:用 action="searchFile.php" 更新了按钮字段 编辑 2:功能 搜索 按钮 编辑 3:小改动

【问题讨论】:

  • 请发布您的ajaxFunction();,如果可能的话,您如何/在哪里处理POST 变量。
  • 添加到 index.html
  • 请检查控制台日志并在那里显示错误
  • 我在 Dream Weaver 中编码,它没有控制台或日志功能?当我在浏览器中测试我的页面时,我没有收到任何错误,我的代码中也没有任何错误。
  • 您的成功输出字段不会回显除$search 之外的任何内容。作为一种调试技术,尝试在其中放置一些虚拟文本以查看它是否在输出任何内容(例如echo 'search result: ' . $search;),这样您就可以查看问题出在搜索还是输出。

标签: php jquery html ajax


【解决方案1】:

您正在尝试在不存在的块中查找搜索字符串

    var searchString = $("#search_box").val();

你没有id为search_box的块,只有类search_box,即$(".search_box")

    <input type='text' onclick="ajaxFunction();" name='search' class='search_box' id='search_box'/>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-05-05
    • 2013-08-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-21
    • 1970-01-01
    相关资源
    最近更新 更多