CSDN首页 空间 新闻 论坛 Blog 下载 读书 网摘 搜索 .NET Java 视频 接项目 求职 在线学习 买书 程序员 通知
山寨机中的战斗机! 程序优化工程师到底对IT界有没有贡献
CSDN社区
搜索 收藏 打印 关闭
CSDN社区 >  Web 开发 >  PHP

当我点击一个下拉列表内容,另一个下拉列表的内容会相应改变,应该怎样用PHP做?

楼主perrylee(小强)2002-09-09 18:15:35 在 Web 开发 / PHP 提问

例如我在一个下拉菜单中选择内容为‘CPU’,另一个下拉菜单就显示从数据库寻找所有CPU的类型,用PHP应该怎样做?  
   
  是否需要刷新页面?还是用function?  
   
  新大家教一下小弟,一定给分!  
  问题点数:50、回复次数:14Top

1 楼anziqi(美洲狮)回复于 2002-09-09 18:36:07 得分 10

http://www.csdn.net/expert/topic/974/974562.xml?temp=.3960382  
   
  这是我以前回答的一个问题.  
   
  跟你一样.看看你会找到答案.Top

2 楼phpteam(phpsalon.com)回复于 2002-09-10 08:29:59 得分 10

这不是PHP能做的,是要用javascript做的,PHP只是负责取数据,如果你一定要用PHP做,那也可以啊,刷新Top

3 楼xizi2002(戏子=>影帝)回复于 2002-09-10 08:38:58 得分 0

同意楼上,Top

4 楼perrylee(小强)回复于 2002-09-10 10:08:04 得分 0

TO   phpteam(George):用javascript是怎样做呀?我不会呀!  
   
  TO   anziqi(安子琦):那篇文章很难看呀!Top

5 楼pantech_36(~ 25436 ~)回复于 2002-09-10 12:52:14 得分 10

<javascript>  
  //&cedil;&Atilde;&ordm;&macr;&Ecirc;&yacute;&Oacute;&Atilde;&Oacute;&Uacute;&sup2;ú&Eacute;ú&Ograve;&raquo;&cedil;&ouml;&Ograve;&raquo;&Icirc;&not;&Ecirc;&yacute;×é&pound;&not;&micro;&laquo;&Ograve;&raquo;°&atilde;&Oacute;&Atilde;&Euml;ü×&ouml;&Ntilde;&shy;&raquo;·&sup2;ú&Eacute;ú&Ograve;&raquo;&cedil;&ouml;&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&Iuml;&Acirc;±ê&Icirc;&ordf;&Ecirc;&yacute;&Ouml;&micro;&pound;&not;&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;±ê&iquest;&Eacute;&Icirc;&ordf;'Data','Value','aText'&micro;&Auml;&para;&thorn;&Icirc;&not;&Ecirc;&yacute;×é&iexcl;&pound;    
  function   aClass(aData,   aValue,   aText)   {  
    if   (aClass.arguments.length   <   3)  
              alert("&acute;&laquo;&Egrave;&euml;&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&acute;í&Icirc;ó&pound;&iexcl;")  
          else   {  
              this.Data     =   aData;         //&sup2;ú&Eacute;ú&micro;&Auml;&frac12;&laquo;&Ecirc;&Ccedil;&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&micro;&Auml;value  
              this.Value   =   aValue;       //&sup2;ú&Eacute;ú&micro;&Uacute;&para;&thorn;&cedil;&ouml;&iquest;ò&micro;&Auml;value&Ouml;&micro;  
              this.Text     =   aText;         //&sup2;ú&Eacute;ú&micro;&Uacute;&para;&thorn;&cedil;&ouml;&iquest;ò&micro;&Auml;text&Auml;&Uacute;&Egrave;&Yacute;  
          }  
      }  
  //&cedil;&Atilde;&ordm;&macr;&Ecirc;&yacute;&Oacute;&Atilde;&Oacute;&Uacute;&micro;&Atilde;&micro;&frac12;&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò±&raquo;&Ntilde;&iexcl;&Ouml;&ETH;&para;&Ocirc;&Iuml;ó&micro;&Auml;value&Ouml;&micro;  
  //&pound;¨aSrc=&micro;&Uacute;&Ograve;&raquo;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&para;&Ocirc;&Iuml;ó&pound;&raquo;aDes=&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&para;&Ocirc;&Iuml;ó&pound;&raquo;aClass&Oacute;&Atilde;&Eacute;&Iuml;&Atilde;&aelig;&ordm;&macr;&Ecirc;&yacute;&sup2;ú&Eacute;ú&micro;&Auml;&Ecirc;&yacute;×é&pound;&raquo;&pound;&copy;  
      function   chgDnCombobox(aSrc,   aDes,   aClass,   aRemain){        
          if   (chgDnCombobox.arguments.length   <   3)  
              alert("&acute;&laquo;&Egrave;&euml;&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&acute;í&Icirc;ó&pound;&iexcl;")  
          else   if   (aSrc.type   !=   "select-one")     //&Aring;&ETH;&para;&Iuml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&Ecirc;&Ccedil;&sup2;&raquo;&Ecirc;&Ccedil;&micro;&yen;&Ntilde;&iexcl;&iquest;ò  
              alert("&acute;&laquo;&Egrave;&euml;&micro;&Auml;&sup2;&Icirc;&Ecirc;&yacute;&acute;í&Icirc;ó&pound;&iexcl;")  
          else   if   (aClass   ==   null)  
              alert("aClass&Icirc;&acute;&para;¨&Ograve;&aring;&acute;í&Icirc;ó&pound;&iexcl;")  
          else   {  
              if   (aRemain==null)  
                  aRemain   =   1;  
              chgDnComboboxItem(aSrc.options[aSrc.selectedIndex].value,   aDes,   aClass,   aRemain);  
          }  
      }  
   
      function   chgDnComboboxItem(aValue,   aDes,   aClass,   aRemain){  
          if   (chgDnComboboxItem.arguments.length<3)  
              alert("&sup2;&Icirc;&Ecirc;&yacute;&acute;&laquo;&Egrave;&euml;&acute;í&Icirc;ó&pound;&iexcl;")  
          else   if   (aClass==null)  
              alert("&acute;í&Icirc;ó&pound;&iexcl;aClass&Atilde;&raquo;&Oacute;&ETH;&para;¨&Ograve;&aring;&pound;&iexcl;")  
          else{  
              if   (aRemain==null)  
                  aRemain   =   1;  
              deleteAllComboboxItem(aDes,   aRemain);   //&Eacute;&frac34;&sup3;&yacute;&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&micro;&Auml;&Euml;ù&Oacute;&ETH;option&para;&Ocirc;&Iuml;ó  
              for   (var   i=0;   i<aClass.length;   i++)  
                  if   (aClass[i].Data==aValue)     /*   check   aClass[i].Data   */  
                      if   (aClass[i].Data!="")  
                          addComboboxItem(aDes,   aClass[i].Text,   aClass[i].Value);//&Eacute;ú&sup3;&Eacute;&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&micro;&Auml;option&para;&Ocirc;&Iuml;ó  
  }  
      }  
  //&Eacute;&frac34;&sup3;&yacute;&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&micro;&Auml;&Euml;ù&Oacute;&ETH;option&para;&Ocirc;&Iuml;ó  
      function   deleteAllComboboxItem(aList,   aRemain)   {  
      for   (var   i=aList.options.length;   i   >   aRemain;   i--)  
              aList.options[i]   =   null;  
      }          
  //&Eacute;ú&sup3;&Eacute;&micro;&Uacute;&para;&thorn;&cedil;&ouml;&Iuml;&Acirc;&Agrave;&shy;&iquest;ò&micro;&Auml;option&para;&Ocirc;&Iuml;ó  
      function   addComboboxItem(aList,   aText,   aValue)   {  
          var   aOption   =   new   Option(aText,   aValue);  
      aList.options[aList.options.length]=aOption;  
   
      }  
   
   
  ///PHP拿数具  
  var   f_Set0   =   new   Array(  
  <?php  
  for($i=0;$i<$stateRow;$i++)  
  {  
      echo   "new   aClass('".$state->result[$i][country]."',   '".$state->result[$i][state]."',   '".$state->result[$i][state]."'),";  
  }  
      echo   "new   aClass('".$state->result[$i][country]."',   '".$state->result[$i][state]."',   '".$state->result[$i][state]."')"  
  ?>  
  );  
  function   change_area(aForm)   {  
  chgDnCombobox(aForm.country,   aForm.state,   f_Set0   ,   0)  
  }  
   
  </javascript>  
   
   
   
  互动框  
  <select   name="country"   onChange="change_area(document.dataform)">  
                                                  <option   value="NULL">---    
                                                  <?php   echo   $Select;?>  
                                                  ---</option>  
                                                  <?php  
  for   ($i=0;$i<$countryRow;$i++)  
  if   ($currCompany->result[0][country]==$country->result[$i][country])  
  echo   "<option   value=\"".$country->result[$i][country]."\"   selected>".$country->result[$i][description]."</option>";  
  else  
  echo   "<option   value=\"".$country->result[$i][country]."\">".$country->result[$i][description]."</option>";  
  ?>  
                                              </select>  
   
  根据国家改变省洲,看见SQL部份是拿数据的,你要根据你自己的情况去拿  
  这里主要用到的是javascript  
    <select   name="state">  
                                                  <option   value="NULL">---    
                                                  <?php   echo   $Select;?>  
                                                  ---</option>  
                                                  <?php  
  for   ($i=0;$i<$stateRow;$i++)  
  if   ($currCompany->result[0][state]==$state->result[$i][state])  
  echo   "<option   value=\"".$state->result[$i][state]."\"   selected>".$state->result[$i][state]."</option>";  
  else  
  echo   "<option   value=\"".$state->result[$i][state]."\">".$state->result[$i][state]."</option>";  
  ?>  
                                              </select>Top

6 楼pantech_36(~ 25436 ~)回复于 2002-09-10 12:54:54 得分 0

不好意思有些乱码是中文注解,把它么去掉都不碍事的^_^Top

7 楼juhaoxu(正月初一就过年喽:)回复于 2002-09-10 13:15:35 得分 0

这个也太烦了,我有简单很多的javascript程序Top

8 楼kuanghu(花和尚)回复于 2002-09-10 13:50:08 得分 10

<?  
        require("conn.php");  
        $sql="select   *   from   table1";  
        $ro=mysql_query($sql);  
    ?>  
  <html>  
  <head>  
  <title>新闻添加</title>  
  <meta   http-equiv="Content-Type"   content="text/html;   charset=gb2312">  
  <script   language="javascript">  
  <!--  
          function   check(){  
                    if(document.form1.news_title.value=="")  
                    {  
                            alert("警告:\n没有填写新闻标题!")  
                            document.form1.news_title.select()  
                            return   false  
                            }  
                    if(document.form1.news_sort.value==-1)  
                    {  
                            alert("警告:\n没有选择新闻分类!")  
                            document.form1.news_sort.focus()  
                            return   false  
                            }  
                    if(document.form1.city.value==-1)  
                    {  
                            alert("警告:\n没有选择新闻二级分类!")  
                            document.form1.city.focus()  
                            return   false  
                            }  
                    if(document.form1.news_message.value=="")  
                    {  
                            alert("警告:\n没有填写新闻内容!")  
                            document.form1.news_message.select()  
                            return   false  
                            }  
                     
            document.form1.submit()  
          return   true  
          }  
        function   cls()  
  {  
              document.form1.news_title.value=""  
      document.form1.news_author.value=""  
      document.form1.news_message.value=""  
     
        }  
    function   change_area()  
  {  
  var   item_title   =   new   Array;  
  <?  
  $j=0;  
  while($rp=mysql_fetch_array($ro))  
  {  
  $kk=   "\"".$rp["sort_name"]."&".$rp["sort_name1"]."\"\n";  
  ?>  
  item_title[<?echo   $j;?>]=<?   echo   $kk;?>  
  <?  
  $j=$j+1;  
  }  
  ?>  
  if   (document.form1.city.value!="-1")  
  {  
            for(var   i=0;i<document.form1.city.length;i++)  
            {    
                  document.form1.city.options[0]=   null;  
            }  
  }  
  var   menu;  
  var   j;  
  j=0;  
  for(var   i=0;i<item_title.length;i++)  
  {  
      menu=item_title[i].split("&");  
      if(menu[0]==document.form1.news_sort.value)  
          {  
  document.form1.city.options[j]=   new   Option(menu[1],menu[1]);  
  j++;  
        }  
  }  
  if(j==0)    
  {  
          document.form1.city.options[0]=   new   Option("----请选择分类----","-1");  
  }  
  }  
  //-->  
  </script>  
   
  <link   rel="stylesheet"   href="../../css/1.css"   type="text/css">  
  </head>  
   
  <body   bgcolor="#cccccc"   text="#000000"   topmargin="0"     leftmargin="0">  
  <form   name="form1"   enctype="multipart/form-data"   method="post"   action="savenew.php?mode=ok"   onSubmit="return   check();">  
  <?include("news_title.php")?>  
      <table   width="80%"   border="0"   cellspacing="0"   cellpadding="0"   align="center"   class="word">  
          <tr>    
              <td>   &nbsp;&nbsp;&nbsp;  
                  <center>  
                      <b>新闻添加   </b>  
  </center>  
              </td>  
              <td   rowspan="2">&nbsp;</td>  
          </tr>  
          <tr>  
              <td>  
                  <div   align="right">说明:带<font   color="#FF0000">*</font>为必填项</div>  
              </td>  
          </tr>  
          <tr   valign="top">    
              <td   height="211">    
                  <table   width="100%"   border="0"   cellspacing="0"   cellpadding="0">  
                      <tr>    
                          <td   width="3%"   height="214">&nbsp;</td>  
                          <td   width="97%"   height="214"   valign="top"   bgcolor="#F7F7F7">    
                              <table   width="100%"   border="1"   bgcolor="#cccccc"   bordercolorlight="#000000"   cellspacing="0"   bordercolordark="#FFFFFF"   cellpadding="0"   class="word">  
                                  <tr>    
                                      <td   width="19%">    
                                          <div   align="center">新闻标题<font   color="#FF0000">*</font>:</div>  
                                      </td>  
                                      <td   width="76%">    
                                          <input   type="text"   name="news_title"   size="50">  
                                      </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%">    
                                          <div   align="center">新闻作者:</div>  
                                      </td>  
                                      <td   width="76%">    
                                          <input   type="text"   name="news_author">  
                                      </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%">    
                                          <div   align="center">新闻分类<font   color="#FF0000">*</font>:</div>  
                                      </td>  
                                      <td   width="76%">    
                                          <select   name="news_sort"   size=1   onChange="change_area()">  
                                              <option   value="-1"   selected>----请选择分类----</option>  
                                              <?  
      $sql="select   *   from   table";  
                      $ry=mysql_query($sql);  
      while($rl=mysql_fetch_array($ry))  
      {  
      ?>  
                                              <option   value="<?echo   $rl["sort_id"];?>">    
                                              <?echo   $rl["sort_name"];?>  
                                              </option>  
                                              <?  
      }  
      ?>  
                                          </select>  
                                          <a   href="news_sort.php"   class="word">&lt;&lt;&lt;添加分类   </a>   </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%"   height="13">    
                                          <div   align="center">二级分类<font   color="#FF0000">*</font>:</div>  
                                      </td>  
                                      <td   width="76%"   height="13">    
                                          <select   name="city"   size=1   >  
                                              <option   value="-1"   selected>----请选择分类----</option>  
                                          </select>  
                                          <a   href="news_sort1.php"   class="word">&lt;&lt;&lt;添加二级分类</a>   </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%"   height="7">    
                                          <center>  
                                              新闻专集  
  </center>  
                                      </td>  
                                      <td   width="76%"   height="7">  
                                          <select   name="new_group">  
  <option   value="">---请选择新闻专集---</option>  
            <?  
    $sqll="select   group_name,group_code   from   news_group";  
    $rtk=mysql_query($sqll);  
    while($rpl=mysql_fetch_array($rtk))  
    {  
    ?>  
                                              <option   value="<?echo   $rpl["group_code"];?>"><?echo   $rpl["group_name"];?></option>  
      <?  
      }  
      ?>  
                                          </select>  
                                      </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%"   height="7">  
                                          <center>  
                                              新闻图片:    
                                          </center>  
                                      </td>  
                                      <td   width="76%"   height="7">  
                                          <input   type="file"   name="pic"   size="30">  
                                      </td>  
                                  </tr>  
                                  <tr>    
                                      <td   height="12"   valign="top">    
                                          <div   align="center">新闻内容<font   color="#FF0000">*</font>:</div>  
                                          <div   align="center">&nbsp;</div>  
                                      </td>  
                                      <td>    
                                          <textarea   name="news_message"   cols="50"   rows="9"></textarea>  
                                      </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%">    
                                          <div   align="center">首页显示:</div>  
                                      </td>  
                                      <td   width="76%">    
                                          <input   type="radio"   name="news_on"   value="0"   checked>  
                                          显示    
                                          <input   type="radio"   name="news_on"   value="1">  
                                          不显示   </td>  
                                  </tr>  
                                  <tr>    
                                      <td   width="19%">    
                                          <center>  
                                              语   种:    
                                          </center>  
                                      </td>  
                                      <td   width="76%">    
                                          <input   type="radio"   name="lan"   value="ch"   checked>  
                                          中文    
                                          <input   type="radio"   name="lan"   value="en">  
                                          英文</td>  
                                  </tr>  
                              </table>  
                          </td>  
                      </tr>  
                  </table>  
              </td>  
              <td   height="211"   width="5%">&nbsp;</td>  
          </tr>  
          <tr>    
              <td   colspan="2">    
                  <center>  
                      <input   type="submit"   name="Submit"   value="添加">  
                      <input   type="reset"   name="Submit2"   value="取消">  
                  </center>  
              </td>  
          </tr>  
      </table>  
  </form>  
   
  </body>  
  </html>  
  Top

9 楼ipman(.NET)回复于 2002-09-10 14:01:16 得分 0

把选择部分单独做成一帧,刷新那帧即可Top

10 楼vivanboy(早起的鸟儿有虫吃)回复于 2002-09-10 14:08:45 得分 0

如果数据量很大,建议用隐藏桢或者xmlhttp来取第二层数据Top

11 楼blueraindrop(blueraindrop)回复于 2002-09-10 14:14:01 得分 0

<form   name=form1   action="">  
  <select   name="select1"   onchange="document.form1.submit()">  
  <option   value="computer1">computer1</option>  
  <option   value="computer2">computer2</option>  
  </select>  
  <?  
  $sql=mysql_query("select   *   from   table   where   computer='$select1'");  
  $num=mysql_numrows($sql);  
  if($num){  
  ?>  
  <select   name="select2">  
  <option   value="字段值">字段值</option>  
  </select>  
        <?  
          }  
    ?>  
  </form>Top

12 楼alexxing(赤铸)回复于 2002-09-10 15:58:27 得分 10

俺是第N次看到类似问题了,哈哈  
   
  给你个结构清晰的参考,自己根据需要修改吧  
   
  <select   id=oSel1   onchange=Chg()>  
  <option>   CPU  
  <option>   硬盘  
  <option>   主板  
  </select>  
  <select   id=oSel2>  
  </select>  
   
  <script>  
   
  //二维数据表格(二维数组)  
  var   Table2dim   =   new   Array(  
          new   Array("Intel   PIII",   "AMD   Athlon",   "Intel   Celoron"),  
          new   Array("IBM",   "Westwood",   "Quantum"),  
          new   Array("Asus",   "Gigabyte"),  
          null  
  );  
   
  //Chg()   和   ChgOptions(i)   都是通用的,只要修改上面的   oSel1   的选项和   Table2dim   的内容即可  
  function   Chg()  
  {  
          ChgOptions(document.all.oSel1.selectedIndex);  
  }  
  function   ChgOptions(iSel1)  
  {  
          if   (   0   <=   iSel1   &&   iSel1   <   Table2dim.length   )  
          {  
                  var   oSel2   =   document.all.oSel2,   i;  
   
                  //删除所有旧选项(不知道有没有不用循环的删除办法)  
                  for   (   i   =   oSel2.options.length;   --   i   >=   0;   )  
                          oSel2.options.remove(i);  
   
                  //创建新关联选项  
                  var   Table1dim   =   Table2dim[iSel1];  
                  for   (   i   =   0;   i   <   Table1dim.length;   i   ++   )  
                  {  
                          var   o   =   document.createElement('<option>');  
                          o.text   =   Table1dim[i];  
                          oSel2.options.add(o);  
                  }  
          }  
  }  
   
  //初始化  
  ChgOptions(0);  
  </script>  
  Top

13 楼happyli(忆忆)回复于 2002-09-10 16:55:08 得分 0

alexxing(赤铸)   说的很对  
  上一次我就用的他的方法,很好的  
  现在我还很感激他啊Top

14 楼alexxing(赤铸)回复于 2002-09-11 10:23:16 得分 0

俺没注意:是要用PHP做。前面一部分:  
   
  <select   id=oSel1   onchange=Chg()>  
  <option>   CPU  
  <option>   硬盘  
  <option>   主板  
  </select>  
  <select   id=oSel2>  
  </select>  
   
  <script>  
   
  //二维数据表格(二维数组)  
  var   Table2dim   =   new   Array(  
          new   Array("Intel   PIII",   "AMD   Athlon",   "Intel   Celoron"),  
          new   Array("IBM",   "Westwood",   "Quantum"),  
          new   Array("Asus",   "Gigabyte"),  
          null  
  );  
   
  可以用PHP从数据库中取出数据动态生成  
  (把后面的函数放到单独的.JS文件中,可以提高速度)  
   
  我的方法只适合于数据量有限的情况,但是简单  
   
  如果你有几百种零配件,几百种CPU,就最好用PHP动态交互实现SELECT的更新,这就复杂多了,需要用一个隐藏帧在“幕后”与服务器交互取得数据,再用DHTML更新SELECT的内容Top

相关问题

  • 列表的项目能否改变?
  • 改变CListCtrl列表头的颜色
  • 如何改变ComboBox的列表宽度
  • PHP怎么操作列表框啊??急!!!
  • 怎么使下一个列表框自动随上一个下拉列表框的改变而改变?
  • 如何改变DATACOMBO列表框下拉部分的宽度?。。。
  • 如何改变DATACOMBO列表框下拉部分的宽度???
  • 下拉列表框的内容动态改变问题
  • 怎么改变列表控制每一项的图标?
  • 列表控件怎样改变其行高?

关键词

  • 内容
  • 新闻
  • 下拉列表
  • 显示

得分解答快速导航

  • 帖主:perrylee
  • anziqi
  • phpteam
  • pantech_36
  • kuanghu
  • alexxing

相关链接

  • Web开发类图书

广告也精彩

反馈

请通过下述方式给我们反馈
反馈
提问
网站简介|广告服务|VIP资费标准|银行汇款帐号|网站地图|帮助|联系方式|诚聘英才|English|问题报告
北京创新乐知广告有限公司 版权所有, 京 ICP 证 070598 号
世纪乐知(北京)网络技术有限公司 提供技术支持
Copyright © 2000-2008, CSDN.NET, All Rights Reserved
GongshangLogo