Ajax + php unlimited linkage drop-down menu instance

Source: Internet
Author: User
The infinite Ajax linkage implementation method is to first select a category by the user, and then we use ajax to implement partial data requests to the php page for processing. php returns the data required by the user to htmljs for processing, in this way, the infinite linkage drop-down menu is achieved. First...

The implementation of Ajax infinite Association is to first select a category by the user, and then we use ajax to implement partial data requests to php pages for processing. php returns the data required by the user to html js for processing, in this way, the infinite linkage drop-down menu is achieved.

The first is the Ajax. php file:

I think this file is used to receive data for data processing.

The code is as follows:

Mysql_connect ("localhost", "root ","");
Mysql_select_db ("aaa ");
Mysql_query ("set names 'utf8 '");

I do not need to say that the above code is connected to the database.

The code is as follows:

// Select statement
1. // $ q = mysql_query ("select * from 'newstype 'where 'kid' ='". $ _ POST ['id']. "'");
2. $ SQL = "select * from 'newstype 'where 'kid' ='". $ _ POST ['id']. "'";

$ Q = mysql_query ($ SQL );

The options 1 and 2 above are because I wrote the select statement again and reported an error, and then I wrote it again. The two results are correct. 1 is commented out.

If (mysql_num_rows ($ q )! = 0) {the number of statements to be searched. if it is 0, it indicates that no branch is displayed.

// Remember to add () in $ _ POST []. this is my error.

The code is as follows:
Echo"";

The output select box will be added to the p of the page that will be displayed later.

The code is as follows:

While ($ rs = mysql_fetch_array ($ q )){
Echo"". $ Rs ['name'].""; The value of option value in select must be given because we need to follow this query statement.
}
Echo"";
Echo"

"; Because the infinite pole linkage is followed by a p box to hold the next select box
}
?>


--------------------------------------------------------------------------------

The following is the Ajax. js file var xmlhttp; defines a variable

The code is as follows:

Function createxml () {this part is mainly used to determine the browser
If (window. XMLHttpRequest) {XMLHttpRequest is the built-in property in javascript.
Xmlhttp = new XMLHttpRequest ();
} Else if (window. ActiveXObject ){
Xmlhttp = new ActiveXObject ('Microsoft. xmlhttp ');
}
}
Function fun (n) {this method is used to display the select onchange event call on the page.
Createxml ();
Var id = document. getElementById ("s" + n). value; select the value in the select statement of the corresponding id, which is the ID field in the database we mentioned above.
Xmlhttp. open ("post", "Ajax. php", true); open the request
Xmlhttp. setRequestHeader ("Content-Type", "application/x-www-form-urlencoded ");
// There is a + after the id
Xmlhttp. send ("id =" + id + "& num =" + n); send a request to obtain the value of this id and the value of n and assign them to id and num respectively.
Xmlhttp. onreadystatechange = function () {stylefun (n)}; triggers onreadystatechange to call the stylefun (n) method and set the n parameter
}
Function stylefun (n ){
If (xmlhttp. readyState = 4 & xmlhttp. status = 200) {assign the obtained text value to the p tag of the corresponding id after the acceptance is complete.
// InnerHTML remember that html is capitalized
Document. getElementById ("list" + n). innerHTML = xmlhttp. responseText;
}
}


--------------------------------------------------------------------------------

The following shows the liandong. php page (forgive me for thinking very easily when I start my name)

The code is as follows:

Mysql_connect ("localhost", "root", "") or die ("failed to connect to database ");
Mysql_select_db ("aaa ");
Mysql_query ("set names 'utf8 '");
$ SQL = "select * from 'newstype 'Where 'kid' = 0"; first find the original root directory type
$ Query = mysql_query ($ SQL );
While ($ rs = mysql_fetch_array ($ query )){
$ Arr [] = $ rs;
}

?>




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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.