Index.html page
1 <!DOCTYPE HTML>2 <HTMLLang= "en">3 <Head>4 <MetaCharSet= "Utf-8">5 <title>6 PHP, JQuery, Ajax, and MySQL DB instances7 </title>8 </Head>9 <Body>Ten <form> One Select a User: A <Selectname= "Users"onchange= "Showuser (this.value)"> - <optionvalue= "1"> - John the </option> - <optionvalue= "2"> - Linda - </option> + </Select> - </form> + <P> A <DivID= "Txthint"> at <b> - User Info 'll be listed here. - </b> - </Div> - </P> - <Scriptsrc= "./js/jquery-1.10.2.min.js"></Script> in <Scriptsrc= "Selectuser.js"></Script> - </Body> to </HTML>
View Code
Selectuser.js page
1 functionShowuser (str) {2 $.ajax ({3Type: "GET",4URL: "getuser.php?q=" +str,5DataType: "JSON",6Successfunction(data) {7 //var data = eval (' (' + data + ') ');8 if(data.success) {9$ ("#txtHint"). HTML (data.msg);Ten}Else { One$ ("#txtHint"). HTML ("error occurred:" +data.msg); A } - }, -Errorfunction(JQXHR) { theAlert ("Error occurred:" +jqxhr.status); - }, - }); -}
View Code
getuser.php page
1<?PHP2 Header(' access-control-allow-origin:* ');3 Header(' Access-control-allow-methods:post,get ');4 Header(' Access-control-allow-credentials:true ');5 Header("Content-type:application/json;charset=utf-8"); 6 $q=$_get["Q"];7 $con=mysql_connect(' www.mytest.com ', ' root ', ' root ');8 if(!$con)9 {Ten die(' Could not connect: '.Mysql_error()); One } A mysql_select_db("Test",$con); - $sql= "SELECT * from myguests WHERE id = '".$q."‘"; - $res=mysql_query($sql); the $result="<table border= ' 1 ' > - <tr><th>id</th><th>FirstName</th> -<th>LastName</th><th>email</th></tr> "; - while($row=Mysql_fetch_array($res)) + { - $result=$result. "<tr><td>".$row[' ID ']. "</td><td>".$row[' FirstName ']. "</td><td>".$row[' LastName ']. "</td><td>".$row[' Email ']. "</td></tr>"; + } A $result=$result." </table> "; at $data=Array(' msg ' =$result, ' success ' =true); - EchoJson_encode ($data); - Mysql_close($con); -?>
View Code
PHP, JQuery, Ajax, and MySQL DB instances