一、编程思路:
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); 让数据库完成字符集转换
感谢版主的回复和讲解。让我受益匪浅。而且受你的启发,终于找到问题所在,完善了代码,达到了自己想要的结果。
一、问题所在:
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>

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.

In PHP, use the clone keyword to create a copy of the object and customize the cloning behavior through the \_\_clone magic method. 1. Use the clone keyword to make a shallow copy, cloning the object's properties but not the object's properties. 2. The \_\_clone method can deeply copy nested objects to avoid shallow copying problems. 3. Pay attention to avoid circular references and performance problems in cloning, and optimize cloning operations to improve efficiency.

PHP is suitable for web development and content management systems, and Python is suitable for data science, machine learning and automation scripts. 1.PHP performs well in building fast and scalable websites and applications and is commonly used in CMS such as WordPress. 2. Python has performed outstandingly in the fields of data science and machine learning, with rich libraries such as NumPy and TensorFlow.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

MinGW - Minimalist GNU for Windows
This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Safe Exam Browser
Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft