Jquery + PHP:从第三方 PHP 脚本获取自动完成字符串

发布于 2024-12-20 02:26:26 字数 1504 浏览 0 评论 0原文

Jquery 搜索示例:

<script>
    $(function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $("#tags").autocomplete({
            source: availableTags
        });
    });
</script>

<div class="demo">
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</div>
<!-- End demo -->

<div class="demo-description">
    <p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
    <p>The datasource is a simple JavaScript array, provided to the widget using the source- option.</p>
</div>
<!-- End demo-description -->

基本上有一个带有自动完成内容的变量,这很好,除了我需要一些可能更复杂的东西。我不需要从 var/xml/sql 提供列表,而是需要从第三方 php 脚本发出的 echo 中获取。

该 php 脚本将根据查询回显相应的信息。即:用户搜索 customsearch.php?q=Lemons 它将回显“Pineapples”。

有人可以帮助我吗?

Jquery search by example:

<script>
    $(function() {
        var availableTags = [
            "ActionScript",
            "AppleScript",
            "Asp",
            "BASIC",
            "C",
            "C++",
            "Clojure",
            "COBOL",
            "ColdFusion",
            "Erlang",
            "Fortran",
            "Groovy",
            "Haskell",
            "Java",
            "JavaScript",
            "Lisp",
            "Perl",
            "PHP",
            "Python",
            "Ruby",
            "Scala",
            "Scheme"
        ];
        $("#tags").autocomplete({
            source: availableTags
        });
    });
</script>

<div class="demo">
    <div class="ui-widget">
        <label for="tags">Tags: </label>
        <input id="tags" />
    </div>
</div>
<!-- End demo -->

<div class="demo-description">
    <p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
    <p>The datasource is a simple JavaScript array, provided to the widget using the source- option.</p>
</div>
<!-- End demo-description -->

There's basically a variable with the autocomplete content, and that's great and all, except I need something perhaps a little more complex. Instead of providing a list from a var/xml/sql I need to grab from an echo issued by a third party php script.

That php script will echo out the appropriate information depending on the query. i.e.: the user searches for customsearch.php?q=Lemons it will echo "Pineapples".

Can someone help me?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

捶死心动 2024-12-27 02:26:26

根据您的其他问题,我假设您'正在进行 AJAX 调用来获取搜索结果。将它们加载到数组中并在示例中替换它:

<script>
function GetSearchResults(){
    // make your ajax call here
    $.ajax({
      async: false,
      url: 'customsearch.php?q=Lemons',
      success: function(data) {
        var availableTags = [];
        // build an array from the response data here
        $( "#tags" ).autocomplete({
            source: availableTags
        });
      }
    });
}

$(function() {
    var availableTags = GetSearchResults();
});
</script>

<div class="demo">

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>

</div><!-- End demo -->

<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions     are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-    option.</p>
</div><!-- End demo-description -->

理想情况下,您不会将 async 设置为 false,但如果您不熟悉回调,我会尽量不让您的大脑爆炸。

Based on your other question, I'd assume you're making an AJAX call to get the search results. Load them into an array and replace it in your example:

<script>
function GetSearchResults(){
    // make your ajax call here
    $.ajax({
      async: false,
      url: 'customsearch.php?q=Lemons',
      success: function(data) {
        var availableTags = [];
        // build an array from the response data here
        $( "#tags" ).autocomplete({
            source: availableTags
        });
      }
    });
}

$(function() {
    var availableTags = GetSearchResults();
});
</script>

<div class="demo">

<div class="ui-widget">
<label for="tags">Tags: </label>
<input id="tags" />
</div>

</div><!-- End demo -->

<div class="demo-description">
<p>The Autocomplete widgets provides suggestions while you type into the field. Here the suggestions     are tags for programming languages, give "ja" (for Java or JavaScript) a try.</p>
<p>The datasource is a simple JavaScript array, provided to the widget using the source-    option.</p>
</div><!-- End demo-description -->

Ideally you wouldn't set async to false, but I'm trying not to make your brain explode if you aren't familiar with callbacks.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文