Ajax入门例子

前端之家收集整理的这篇文章主要介绍了Ajax入门例子前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在customer.PHP文件中,代码如下:@H_403_1@ @H_403_1@ @H_403_1@ <html>@H_403_1@ <head>@H_403_1@ @H_403_1@ @H_403_1@ <script type="text/javascript">@H_403_1@ function showCustomer(str)@H_403_1@ {@H_403_1@ var xmlhttp; @H_403_1@ @H_403_1@ @H_403_1@ if (str=="")@H_403_1@ {@H_403_1@ document.getElementById("txtHint").innerHTML="";@H_403_1@ return;@H_403_1@ }@H_403_1@ if (window.XMLHttpRequest)@H_403_1@ {// code for IE7+,Firefox,Chrome,Opera,Safari@H_403_1@ xmlhttp=new XMLHttpRequest();@H_403_1@ }@H_403_1@ else@H_403_1@ {// code for IE6,IE5@H_403_1@ xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");@H_403_1@ }@H_403_1@ xmlhttp.onreadystatechange=function()@H_403_1@ {@H_403_1@ if (xmlhttp.readyState==4 && xmlhttp.status==200)@H_403_1@ {@H_403_1@ document.getElementById("txtHint").innerHTML = xmlhttp.responseText;@H_403_1@ }@H_403_1@ }@H_403_1@ xmlhttp.open("GET","/test/getcustomer.PHP?q="+str,true);//这里的url是指你的文件路径,例如,我的customer.PHP和getcustomer.PHP都在www下的test文件 目录下,所以就写成了此种形式@H_403_1@ xmlhttp.send();@H_403_1@ }@H_403_1@ </script>@H_403_1@ @H_403_1@ @H_403_1@ </head>@H_403_1@ <body>@H_403_1@ <form action="" style="margin-top:15px;">@H_403_1@ <label>请选择一位客户:@H_403_1@ <select name="customers" onchange="showCustomer(this.value)" style="font-family:Verdana,Arial,Helvetica,sans-serif;">@H_403_1@ <option value="APPLE">Apple Computer,Inc.</option>@H_403_1@ <option value="BAIDU ">BAIDU,Inc</option>@H_403_1@ <option value="Canon">Canon USA,Inc.</option>@H_403_1@ <option value="Google">Google,Inc.</option>@H_403_1@ <option value="Nokia">Nokia Corporation</option>@H_403_1@ <option value="SONY">Sony Corporation of America</option>@H_403_1@ </select>@H_403_1@ </label>@H_403_1@ </form>@H_403_1@ <br />@H_403_1@ <div>客户信息将在此处列出 ...</div>@H_403_1@ <div id="txtHint"></div>@H_403_1@ @H_403_1@ @H_403_1@ </body>@H_403_1@ </html>@H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@ @H_403_1@

在getcustomer.PHP文件中,代码如下:

@H_403_1@

<?PHP $q=$_GET["q"]; $con = MysqL_connect('localhost','root','root'); if (!$con) { die('Could not connect: ' . MysqL_error()); } MysqL_select_db("test",$con); $sql="SELECT * FROM user1 WHERE firstname= '".$q."'"; //var_dump($sql); $result = MysqL_query($sql); echo "<table border='1'> <tr> <th>Firstname</th> <th>Lastname</th> <th>Age</th> <th>Hometown</th> <th>Job</th> </tr>"; while($row = MysqL_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['firstname'] . "</td>"; echo "<td>" . $row['lastname'] . "</td>"; echo "<td>" . $row['age'] . "</td>"; echo "<td>" . $row['hometown'] . "</td>"; echo "<td>" . $row['Job'] . "</td>"; echo "</tr>"; } echo "</table>"; MysqL_close($con); ?> //数据库是test, 表是user1,一共有五个字段,分别是firstname,lastname,age,hometown和Job 最后能实现页面无刷新字段的变化的效果!刚学Ajax,感觉很不错!

猜你在找的Ajax相关文章