一、编程思路:
1. 使用xajax。
2. 从mysql数据库的nation表中获得各省的名称字段“province”.
3. 将该字段数组元素分别作为下拉列表的选项.(即列表项分别为:北京,天津,河南,河北……等)
4. 点击按钮“换吧”,之后,将下拉列表显示在
<?php // a部分:从数据库中取得province字段值include_once("../connections/fycon.php");mysql_select_db($database_fycon,$fycon); $sqlone="select * from nation where province<>''";mysql_query("set names gb2312",$fycon);$rstone=mysql_query($sqlone,$fycon) or die(mysql_error());// a部分:结束。// ↓ 取得结果集的行数。$rows=mysql_num_rows($rstone);// ↓ 构建空字串,准备存放javascript代码$theoptions="";// b部分:用数据库结果集构建下拉列表框// 思路:将所有代码以字串形式放进变量$theopotions中,// 将来接用xmlhttpresponse返回该字串,并显示在<div id="yes"></div>中。$theoptions.= "<form name=\"ok\" id=\"ok\">";$theoptions.= "<select>";while($rows){ $getvalue=mysql_fetch_assoc($rstone); //echo "<option value=\"".$getvalue['province']."\">".$getvalue['province']."</option>"; $theoptions.= "<option value=\"".$getvalue['province']."\">".$getvalue['province']."</option>"; $rows=$rows-1;}$theopotions.= "</select>";$theopotions.= "</form>";// b部分:结束?>// 下面是xajax部分<?php include_once("../xajax/xajax.inc.php");$myxajax= new xajax();$myfunct=$myxajax->register(xajax_function,"myfunc");$myxajax->processrequest();function myfunc($arg){ $theoptions=iconv("gb2312","utf-8",$theoptions); //转码,将utf-8转为gb2312 $objresponse= new xajaxresponse(); $objresponse->assign('yes','innerhtml',$theoptions); return $objresponse;}// xajax部分结束?><!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=gb2312" /><title>?</title></head><?php $myxajax->printjavascript("../xajax");?><body ><br/><br/><div id="yes" style="width:897px; height:600px; background-color:#f00; margin-top:0px; margin-right:auto; margin-bottom:0px; margin-left:auto"></div><form><input type="button" value="换吧" onclick="xajax_myfunc('1');"/></form></body></html>
function myFunc($arg){
$theOptions=iconv("gb2312","utf-8",$theOptions); //转码,将utf-8转为gb2312
$objResponse= new xajaxResponse();
$objResponse->assign('yes','innerHTML',$theOptiones);
return $objResponse;
}
iconv("gb2312","utf-8",$theOptions) 是将 $theOptions 从 gb2312 转到 utf-8,确认是要这样吗(与你的注释是相反的)
$theOptions 不是传入的,也没有赋值
你的程序中并没有中文字符串,所以可 mysql_query("set names utf8",$fycon); 让数据库完成字符集转换
function myFunc($arg){
$theOptions=iconv("gb2312","utf-8",$theOptions); //转码,将utf-8转为gb2312
$objResponse= new xajaxResponse();
$objResponse->assign('yes','innerHTML',$theOptiones);
return $objResponse;
}
iconv("gb2312","utf-8",$theOptions) 是将 $theOptions 从 gb2312 转到 utf-8,确认是要这样吗(与你的注释是相反的)
$theOptions 不是传入的,也没有赋值
你的程序中并没有中文字符串,所以可 mysql_query("set names utf8",$fycon); 让数据库完成字符集转换
<?php // A部分:从数据库中取得province字段值include_once("../Connections/fycon.php");mysql_select_db($database_fycon,$fycon); $sqlone="select * from nation where province<>''";mysql_query("set names gb2312",$fycon);$rstone=mysql_query($sqlone,$fycon) or die(mysql_error());// A部分:结束。// ↓ 取得结果集的行数。$rows=mysql_num_rows($rstone);// ↓ 构建空字串,准备存放javascript代码$theOptions="";// B部分:用数据库结果集构建下拉列表框// 思路:将所有代码以字串形式放进变量$theOptions中,// 将来接用XMLHttpResponse返回该字串,并显示在<div id="yes"></div>中。$theOptions.= "<form name=\"ok\" id=\"ok\">";$theOptions.= "<select>";while($rows){ $getvalue=mysql_fetch_assoc($rstone); //echo "<option value=\"".$getvalue['province']."\">".$getvalue['province']."</option>"; $theOptions.= "<option value=\"".$getvalue['province']."\">".$getvalue['province']."</option>"; $rows=$rows-1;}$theOpotions.= "</select>";$theOpotions.= "</form>";// B部分:结束?>// 下面是xajax部分<?php include_once("../xajax/xajax.inc.php");$myXajax= new xajax();$myFunct=$myXajax->register(XAJAX_FUNCTION,"myFunc");$myXajax->processRequest();function myFunc($arg){ global $theOptions; //与错误代码相比,就加了这么一句,解决了问题。 $theOptions=iconv("gb2312","utf-8",$theOptions); //转码,将gb2312转为 utf-8 $objResponse= new xajaxResponse(); $objResponse->assign('yes','innerHTML',$theOptions); return $objResponse;}// xajax部分结束?><!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=gb2312" /><title>?</title></head><?php $myXajax->printJavascript("../xajax");?><body ><br/><br/><div id="yes" style="width:897px; height:600px; background-color:#F00; margin-top:0px; margin-right:auto; margin-bottom:0px; margin-left:auto"></div><form><input type="button" value="换吧" onclick="xajax_myFunc('1');"/></form></body></html>
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号