佳木斯湛栽影视文化发展公司

主頁(yè) > 知識(shí)庫(kù) > Ajax+PHP實(shí)現(xiàn)的分類列表框功能示例

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

熱門標(biāo)簽:團(tuán)購(gòu)網(wǎng)站 Mysql連接數(shù)設(shè)置 科大訊飛語(yǔ)音識(shí)別系統(tǒng) 電子圍欄 服務(wù)器配置 銀行業(yè)務(wù) Linux服務(wù)器 阿里云

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

一 代碼

conn.php:

?php
  $conn = mysql_connect("localhost", "root", "root") or die("連接數(shù)據(jù)庫(kù)服務(wù)器失?。?.mysql_error()); //連接MySQL服務(wù)器
  mysql_select_db("db_database27",$conn); //選擇數(shù)據(jù)庫(kù)db_database27
  mysql_query("set names utf8"); //設(shè)置數(shù)據(jù)庫(kù)編碼格式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ù)庫(kù)連接文件
 $sql=mysql_query("select * from tb_commotype group by ptype");//按大類分組查詢
 while($row=mysql_fetch_array($sql)){//循環(huán)輸出下拉列表框選項(xiàng)
  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">商品價(jià)格:/td>
   td height="26" colspan="2" bgcolor="#FFFFFF">input type="text" name="price" />/td>
  /tr>
  tr>
   td height="26" bgcolor="#FFFFFF">nbsp;/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);//頁(yè)面載入即執(zhí)行函數(shù),顯示子類內(nèi)容
/script>
/body>
/html>

type.php:

?php
  include_once("conn/conn.php");//包含數(shù)據(jù)庫(kù)連接文件
 //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)輸出列表框選項(xiàng)中子類內(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對(duì)象
  xml=new ActiveXObject('Microsoft.XMLHTTP');
 }else if(window.XMLHttpRequest){//如果瀏覽器支持XMLHttpRequest對(duì)象則創(chuàng)建XMLHttpRequest對(duì)象
  xml=new XMLHttpRequest();
 }
  xml.open("GET","type.php?ptype="+v,true);//使用GET方法調(diào)用type.php并傳遞參數(shù)的值
  xml.onreadystatechange=function(){//當(dāng)服務(wù)器準(zhǔn)備就緒執(zhí)行回調(diào)函數(shù)
   if(xml.readyState==4  xml.status==200){//如果服務(wù)器已經(jīng)傳回信息并未發(fā)生錯(cuò)誤
    var msg=xml.responseText;//把服務(wù)器傳回的值賦給變量msg
 //document.getElementById("showtype").innerHTML=msg;
 alert(msg);
   showtype.innerHTML=msg;//把傳回的值顯示在id=showtype的元素中
   }
  }
  xml.send(null);//不發(fā)送任何數(shù)據(jù),因?yàn)閿?shù)據(jù)已經(jīng)使用請(qǐng)求URL通過(guò)GET方法發(fā)送
}

二 運(yùn)行結(jié)果

更多關(guān)于PHP相關(guān)內(nèi)容感興趣的讀者可查看本站專題:《PHP+ajax技巧與應(yīng)用小結(jié)》、《PHP網(wǎng)絡(luò)編程技巧總結(jié)》、《PHP基本語(yǔ)法入門教程》、《php面向?qū)ο蟪绦蛟O(shè)計(jì)入門教程》、《php字符串(string)用法總結(jié)》、《php+mysql數(shù)據(jù)庫(kù)操作入門教程》及《php常見(jiàn)數(shù)據(jù)庫(kù)操作技巧匯總》

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

您可能感興趣的文章:
  • PHP+Ajax檢測(cè)用戶名或郵件注冊(cè)時(shí)是否已經(jīng)存在實(shí)例教程
  • PHP+AJAX實(shí)現(xiàn)無(wú)刷新注冊(cè)(帶用戶名實(shí)時(shí)檢測(cè))
  • php注冊(cè)系統(tǒng)和使用Xajax即時(shí)驗(yàn)證用戶名是否被占用
  • PHP+Ajax異步通訊實(shí)現(xiàn)用戶名郵箱驗(yàn)證是否已注冊(cè)( 2種方法實(shí)現(xiàn))
  • jQuery ajax+PHP實(shí)現(xiàn)的級(jí)聯(lián)下拉列表框功能示例
  • Ajax+PHP實(shí)現(xiàn)的刪除數(shù)據(jù)功能示例
  • Ajax+PHP實(shí)現(xiàn)的模擬進(jìn)度條功能示例
  • PHP+Ajax實(shí)現(xiàn)的檢測(cè)用戶名功能簡(jiǎn)單示例

標(biāo)簽:廣元 衡水 萍鄉(xiāng) 蚌埠 衢州 大理 棗莊 江蘇

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《Ajax+PHP實(shí)現(xiàn)的分類列表框功能示例》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    都昌县| 望奎县| 安龙县| 玛纳斯县| 梁河县| 齐齐哈尔市| 阿拉善左旗| 锦州市| 中西区| 南木林县| 牡丹江市| 蒙自县| 新民市| 澎湖县| 云龙县| 南宫市| 比如县| 琼中| 韩城市| 开封市| 潜山县| 浙江省| 长白| 邯郸市| 龙游县| 巴南区| 永川市| 博野县| 松原市| 邵武市| 新巴尔虎右旗| 黄山市| 开阳县| 城固县| 合江县| 探索| 宁德市| 漾濞| 德清县| 宜阳县| 勃利县|