自动完成用PHP [英] Autocomplete with PHP

查看:116
本文介绍了自动完成用PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

它我的html code:

 < HTML和GT;
< HEAD>
<脚本类型=文/ JavaScript的SRC =jquery.js和>< / SCRIPT>
<脚本类型=文/ JavaScript的SRC =jqueryui.js>< / SCRIPT>
    <脚本>
        $(#tags).autocomplete({
        网址:'?Ajax.php TXT ='
        });
    < / SCRIPT>
< /头>
<身体GT;
< D​​IV CLASS =演示>
< D​​IV CLASS =UI窗口小部件>
    <标签=标签>标签:LT; /标签>
    <输入ID =标签NAME =TXT/>
< / DIV>
< / DIV>
< /身体GT;
< / HTML>

和这里是我的Ajax.php

 &LT ;?
    $ VAL = $ _REQUEST [TXT];
    如果($ VAL ==251){回声的WoooW;}
    其他
    回声没有找到;
?>

但它不工作的自动完成。
什么是我的错?


解决方案

您必须包装你的jQuery成这样的功能:

 <脚本>
    $(函数(){
        $(#tags).autocomplete({
            来源:Ajax.php
            的minLength:2
        });
    });
< / SCRIPT>

你必须从Ajax.php返回json_en $ C $镉含量(例)
它指出在文档中:


  

当一个字符串被使用(如不是一套数组),自动完成插件预计字符串
  指向一个网址资源将返回JSON数据。它可以是对
  同一主机或不同的一个(必须提供JSONP)。请求
  参数术语被添加​​到该URL。


 < PHP
$ VAL = $ _REQUEST [术语];如果($ VAL ==251){
    $收益=其WoooW 251;
} ELSEIF($ VAL ==123){
    $收益=其WoooW 123;
}其他{
    $收益=未找到;
}回声json_en code($返程);
?>

Its my html code :

<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="jqueryui.js"></script>
    <script>
        $( "#tags" ).autocomplete({
        url: 'Ajax.php?txt='        
        });
    </script>
</head>
<body>
<div class="demo">
<div class="ui-widget">
    <label for="tags">Tags: </label>
    <input id="tags" name="txt" />
</div>
</div>
</body>
</html>

and here is my Ajax.php

<? 
    $val = $_REQUEST["txt"];
    if($val == "251") { echo "its WoooW";}
    else 
    echo "Nothing found";
?>

But it's not working for autocomplete. What is my mistake?

解决方案

You must wrap your jquery into a function like this:

<script>
    $(function() {
        $( "#tags" ).autocomplete({
            source: "Ajax.php",
            minLength: 2
        });
    });
</script>

And you must return json_encoded content from Ajax.php (Example) It States within the documentation:

When a String is used (eg not a set array), the Autocomplete plugin expects that string to point to a URL resource that will return JSON data. It can be on the same host or on a different one (must provide JSONP). The request parameter "term" gets added to that URL.

<?php
$val = $_REQUEST["term"];

if($val == "251") { 
    $return="its WoooW 251";
}elseif($val == "123"){
    $return="its WoooW 123";
}else{
    $return="Nothing Found";
}

echo json_encode($return);
?>

这篇关于自动完成用PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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