Php + ajax Real-time get drop-down data program code

Source: Internet
Author: User
Tags php code

After you click the desired data, the data is written to the current input box.
Separate them with commas (,). When the input is continued, the background processes the output data for selection.

Next let's look at the example, html code

The code is as follows: Copy code

<Meta http-equiv = "Content-Type" content = "text/html; charset = utf-8"/>
Ajax Real-time acquisition of drop-down data </pre>
<Style> <! --
. Input_s {position: relative}
. Input_s ul {list-style: none; margin: 0; padding: 0; width: 200px; border: 1px solid # ccc; border-bottom: none}
. Input_s ul li {border-bottom: 1px solid # ccc}
. Input_s ul li: hover {color: # fff; background: #000}
--> </Style>
<Pre> </pre>
<Div class = "input_s"> <input class = "tla" id = "tla" style = "width: 500px;" type = "text" name = "tla"/>
</Div>
<Pre>
<Script type = "text/javascript"> // <! [CDATA [
Var funjieliu = function (fn, delay) {// function throttle add by shanmao 2013-1-18
Var timer = null;
Return function (){
Var context = this, args = arguments;
ClearTimeout (timer );
Timer = setTimeout (function (){
Fn. apply (context, args );
}, Delay );
};
};
Document. getElementById ("tla"). onkeyup = funjieliu (function () {// when the keyboard is pressed
Var tla = $ ("# tla"). val ();
If (tla ){
$. Post ("/cityosweb/default. php/shanmao/input_xiala", {tla: tla}, function (data ){
If (data. status = 1 ){
$ (". Inul" developer.html ("");
$. Each (data. data, function (index, val ){
$ (". Inul"). append ("
<Li> "+ val. username +" </li>
 
");
});
            }
}, "Json ");
        }
},500 );
$ (Function (){
$ (". Inul li"). live ("click", function (){
Var thval = values (this).html ();
Var tla = $ ("# tla"). val ();
Var regexp = new RegExp (",");
If (regexp. test (tla) {// if input has a value (","), add the value in input.
$ ("# Tla"). val (tla + thval + ",");
} Else {
$ ("# Tla"). val (thval + ",");
                }
$ (". Inul" developer.html ("");
$ ("# Tla"). focus ();
});
});
//]> </Script>

Php code above

The code is as follows: Copy code

Function input_xiala (){
$ Input = new input ();
$ Tval = $ input-> post ('tla ');
$ U = M ('user ');
If (strpos ($ tval, ",") {// check whether ","
$ Val = explode (",", $ tval); // split it into an array
$ Tval = end ($ val); // the last value of the array
        }
$ Re = $ u-> field ('username, email ')-> where ("username like' $ tval % '")-> limit (10) -> select ();
$ This-> ajaxReturn ($ re, 'success', 1 );
    }

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.