国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 語言 > PHP > 正文

Ajax+PHP實現(xiàn)的分類列表框功能示例

2024-05-05 00:06:31
字體:
供稿:網(wǎng)友

本文實例講述了Ajax+PHP實現(xiàn)的分類列表框功能。分享給大家供大家參考,具體如下:

一 代碼

conn.php:

<?php  $conn = mysql_connect("localhost", "root", "root") or die("連接數(shù)據(jù)庫服務(wù)器失敗!".mysql_error()); //連接MySQL服務(wù)器  mysql_select_db("db_database27",$conn); //選擇數(shù)據(jù)庫db_database27  mysql_query("set names utf8"); //設(shè)置數(shù)據(jù)庫編碼格式utf8?>

index.php:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>添加商品信息</title></head><body><script language="javascript" src="index.js"></script><form name="form" method="post" action=""> <table width="419" border="0" align="center" cellspacing="1" bgcolor="#9999CC">  <tr>   <td height="36" colspan="3" bgcolor="#FFFFFF"><font color="#0066CC" size="+2">添加商品</font></td>  </tr>  <tr>   <td width="122" height="26" bgcolor="#FFFFFF" align="right">商品名稱:</td>   <td height="26" colspan="2" bgcolor="#FFFFFF"><input type="text" name="name" /></td>  </tr>  <tr>   <td height="26" bgcolor="#FFFFFF" align="right">商品類別:</td>   <td width="64" height="26" bgcolor="#FFFFFF"><select name="ptype" id="ptype" onchange="changetype(this.value)">  <?php   include_once("conn/conn.php");//包含數(shù)據(jù)庫連接文件 $sql=mysql_query("select * from tb_commotype group by ptype");//按大類分組查詢 while($row=mysql_fetch_array($sql)){//循環(huán)輸出下拉列表框選項  echo "<option value='".$row['ptype']."'>".$row['ptype']."</option>"; } ?>   </select></td>   <td width="219" height="26" bgcolor="#FFFFFF" id="showtype" name="showtype"></td>  </tr>  <tr>   <td height="26" bgcolor="#FFFFFF" align="right">商品價格:</td>   <td height="26" colspan="2" bgcolor="#FFFFFF"><input type="text" name="price" /></td>  </tr>  <tr>   <td height="26" bgcolor="#FFFFFF"> </td>   <td height="26" colspan="2" bgcolor="#FFFFFF"><input type="submit" name="Submit" value="提交" /></td>  </tr> </table></form><script language="javascript">  changetype(document.getElementById("ptype").value);//頁面載入即執(zhí)行函數(shù),顯示子類內(nèi)容</script></body></html>

type.php:

<?php  include_once("conn/conn.php");//包含數(shù)據(jù)庫連接文件 //echo $_GET['ptype']; //$ptype=iconv("gb2312","utf-8",$_GET['ptype']);//把參數(shù)值做編碼轉(zhuǎn)換 $sql=mysql_query("select stype from tb_commotype where ptype='".$_GET['ptype']."'");//查詢子類內(nèi)容 echo "<select name='stype' id='stype'>";//輸出html while($row=mysql_fetch_array($sql)){//循環(huán)輸出列表框選項中子類內(nèi)容  echo "<option value='".$row['stype']."'>".$row['stype']."</option>"; } echo "</select>";//輸出html?>

index.js:

function changetype(v){ var xml; if(window.ActiveXObject){//如果是瀏覽器支持ActiveXObjext則創(chuàng)建ActiveXObject對象  xml=new ActiveXObject('Microsoft.XMLHTTP'); }else if(window.XMLHttpRequest){//如果瀏覽器支持XMLHttpRequest對象則創(chuàng)建XMLHttpRequest對象  xml=new XMLHttpRequest(); }  xml.open("GET","type.php?ptype="+v,true);//使用GET方法調(diào)用type.php并傳遞參數(shù)的值  xml.onreadystatechange=function(){//當服務(wù)器準備就緒執(zhí)行回調(diào)函數(shù)   if(xml.readyState==4 && xml.status==200){//如果服務(wù)器已經(jīng)傳回信息并未發(fā)生錯誤    var msg=xml.responseText;//把服務(wù)器傳回的值賦給變量msg //document.getElementById("showtype").innerHTML=msg; alert(msg);   showtype.innerHTML=msg;//把傳回的值顯示在id=showtype的元素中   }  }  xml.send(null);//不發(fā)送任何數(shù)據(jù),因為數(shù)據(jù)已經(jīng)使用請求URL通過GET方法發(fā)送}

二 運行結(jié)果

Ajax,PHP,分類列表框

希望本文所述對大家PHP程序設(shè)計有所幫助。


注:相關(guān)教程知識閱讀請移步到PHP教程頻道。
發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表

圖片精選

主站蜘蛛池模板: 怀宁县| 岳西县| 铜陵市| 五常市| 赫章县| 勐海县| 景德镇市| 周至县| 汕头市| 社会| 多伦县| 苍梧县| 泽库县| 浮山县| 涿鹿县| 康马县| 云龙县| 湛江市| 新郑市| 建昌县| 平度市| 壶关县| 永济市| 招远市| 万盛区| 休宁县| 勃利县| 博湖县| 长乐市| 鄢陵县| 汶上县| 惠州市| 罗甸县| 达州市| 渝中区| 常熟市| 个旧市| 卢龙县| 淮北市| 鄂托克前旗| 阜康市|