【xajax】请看下面的程序,没有出现想要的结果,什么情况?

WBOY
Freigeben: 2016-06-23 13:23:46
Original
840 Leute haben es durchsucht

一、编程思路:
    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>
Nach dem Login kopieren



回复讨论(解决方案)

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); 让数据库完成字符集转换



感谢版主的回复和讲解。让我受益匪浅。而且受你的启发,终于找到问题所在,完善了代码,达到了自己想要的结果。

一、问题所在:
1. 用于返回的字符串变量:$theOptions并没有传入myFunc()函数。
2. 虽然看上去在myFunc()函数中也有一个$theOptions变量,但此变量非彼变量,函数内外的两个$theOptions变量并非同一个变量。【从内存角度来看,这两个变量代表不同的内存地址,根本就不是同一个变量】。
3. 换句话说:函数myFunc()内的$theOptions变量,相当于在函数内新定义的一个$theOptions变量,和函数外的那个同名变量没有一毛钱关系,就好像两个同名同姓的人一样,虽然同名同姓,但根本不是同一个人。

二、问题的解决:
1. 要想将函数外的$theOptions变量在函数内使用,则要让它成为一个全局变量,这样,就如同“同一个人,从函数外进入到了函数内” 。
2. 具体到代码,就是在myFunc()函数中加一行“global $theOptions;”。
3. 以前之所以没有显示“下拉列表框”,就是因为没有将函数外定义和赋值的$theOptions变量传入函数内,而是在函数内新定义了一个没有赋过值的$theOptions同名变量。既然这个新变量啥值也没有,怎么可能显示“下拉列表框”呢?
4. 另外,那句:$theOptions=iconv("gb2312","utf-8",$theOptions); 也是必要的,版主指出我的注释写错了。的确如此,注释将此句的作用写反了,应该是:将gb2312转码为utf-8。之所以说这句必不可少是因为,如果不进行转码,那么仍然会出现不显示下拉列表框的情况。

三、 总结:
1. 越是让人百思不得其解的错误,越是低级错误。
2. 有时,程序没有逻辑错误,但人脑出现了逻辑错误,程序逻辑≠人脑逻辑,就会出现“所得非所想”的结果。
3. 这段错误代码虽然披着xajax的外衣,但实际却是一个普通的低级语法错误,搞混了变量的作用域。
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部分:用数据库结果集构建下拉列表框// 思路:将所有代码以字串形式放进变量$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>
Nach dem Login kopieren
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!