Maison >développement back-end >tutoriel php >【xajax】请看下面的程序,没有出现想要的结果,什么情况?
一、编程思路:
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>