1 <! DOCTYPE htmlPublic "-//W3C//DTD XHTML 1.0 transitional//en" "Http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd" >2 3 4 <meta http-equiv= "Content-type" content= "text/html; Charset=utf-8 "/>5 <title> Ajax </title>6 <script src= ". /jquery-1.11.2.min.js "></script>Reference jquery Package7 89 <body>Ten <select id= "sel" >1112</select></body><!--The Ajax way is equivalent to a factory, give him something to return to something I want16The process of conversion between this need to operate the database, then we need to do a dedicated page to operate the database17Then JS this end is only responsible for the processing page to pass a value and receive a processing page return value18 The function of the processing page is to receive the passed value and output the corresponding result and return<script type= "Text/javascript" >20$ (document). Ready (function(e) {22//Using AJAX requires calling the Ajax method inside jquery23 $.Ajax ({24//Format is fixed do not scribbleURL: "chuli.php",//Connect to Processing pagedata:{},//The value to commitType: "POST",//How to submitDataType: "TEXT",//return typeSuccess:function (s) {//callback function, what to do after success30var hang = S.Split ("|");//Now split the string by the | To split the line.31var str = "";//Create an empty string variable to store a string that splits into a coarse32Forvar i=0;i)33{34var lie = hang[i].Split ("^");// after looping the ^ symbol to split the 35 str = str+ "<option value= ' +lie[0]+ ' > "+lie[1]+" </option> "36 }37 $ ("#sel"). html (str); // Throw the split string to the unit with ID sel 39 }40 41 }); 42 43 44 }); 45 46 47 </script >48
chuli.php page Code
1 <?Php2$db =New Mysqli ("localhost", "root", "" "," AAAAA ");3!Mysqli_connect_error () orDie ("Connection failed! ");4$sql = "SELECT * FROM Car";5$r =$db->query ($sql);6$attr =$rFetch_all ();7$str = "";8foreach$attrAs$v)9{10 $str = $str. implode ("^ ", $v ); 11 $str = $str. "|" 12 }13 14 $str = substr ( $str, 0,strlen ( $str) -115 16 echo $str;
The return value is the Ajax method of text