填充下拉 - PHP阿贾克斯MySQL的 [英] Populating dropdown - PHP Ajax MySQL

查看:112
本文介绍了填充下拉 - PHP阿贾克斯MySQL的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个下拉菜单在我的HTML页面:第一个下拉包含一个基于数据库列名其上的第二个下拉将填充

我有一个领域一个表:<学生姓名,学历,城市> 和以下将是条目;

  1。 A,BS,纽约
2.B,BS,芝加哥
3.C,MS,波士顿
4.D,MS,纽约
 

所以,我的第一个下拉列表将包含列名,即度和城市。

如果我选择的度,第二个下拉应填写BS和MS如果我选择城市,第二个下拉应选择纽约,波士顿和芝加哥。

我怎么能去与执行?

[添加我的code]:

changeSecond(第一)方法完全一样如你所说

 <身体GT;
   <形式方法=POST行动=NAME =MainForm的>
      <表>
         &其中; TR>
            < TD>过滤:< / TD>
            < TD>
            < D​​IV ID =第一>
               <选择的onChange =changeSecond(THIS.VALUE)>
                  <期权价值=1>所有< /选项>
                  <期权价值=2>度< /选项>
                  <期权价值=3>城市和LT; /选项>
               < /选择>
            < / TD>
         < / TR>
         &其中; TR>
            < TD>&安培; NBSP< / TD>
            < TD>
               < D​​IV ID =第二>
                  <选择名称=VAL>
                     <期权价值=>< /选项>
                  < /选择>
               < / DIV>
            < / TD>
         < / TR>
      < /表>
   < /形式GT;
< /身体GT;
 

这是second_script.php如你所说:

 &LT ;?
   $链接=的mysql_connect(localhost的根,);

   如果(!$链接)
   {
      死亡(无法连接:mysql_error());
   }
   如果(mysql_select_db(MyDatabase的,$链接))
   {
      $第一= mysql_real_escape_string($ _ REQUEST [第一]);
      $查询=选择$第一FROM mytable的GROUP BY第一个$。;
      $数据=请求mysql_query($查询);

      回声<选择ID = \第二个\>中;
      而($行= mysql_fetch_row($数据))
      {
         回声。<期权价值= \$行[0]\>$行[0]。< /选项>中;
      }
      回声< /选择&g​​t;中;
   }
   回声mysql_error();
?>
 

解决方案

如果你想要一个更动态的解决方案(将适应变化的背景DB),你可以做这样的事情在页面上:

 <脚本>
        功能changeSecond(第一){
        VAR XMLHTTP;
        如果(window.XMLHtt prequest)
          {// $ C $下IE7 +,火狐,Chrome,歌剧,Safari浏览器
          XMLHTTP =新XMLHtt prequest();
          }
        其他
          {// code对IE6,IE5
          XMLHTTP =新的ActiveXObject(Microsoft.XMLHTTP);
          }
        xmlhttp.onreadystatechange =功能()
        {
          如果(xmlhttp.readyState == 4和&安培; xmlhttp.status == 200)
            {
            VAR解析度= xmlhttp.responseText;
            。的document.getElementById(二)的innerHTML =资源;
            }
          }
        xmlhttp.open(GET,second_script.php先=?+第一,真正的);
        xmlhttp.send();
        }
        < / SCRIPT>
...
<选择的onChange =changeSecond(THIS.VALUE)>
<期权价值=度>度< /选项>
<期权价值=城与GT;城市和LT; /选项>
< /选择>
< D​​IV ID =第二><选择><期权价值=>< /选项>< /选择>< / DIV>
 

,然后类似的脚本:

 < PHP
//数据库连接
$第一= mysql_real_escape_string($ _ REQUEST [第一]);
$查询=选择$第一FROM表名GROUP BY$第一。;
$数据=请求mysql_query($查询);
回声<选择>中;
而($行= mysql_fetch_row($数据)){
回声。<期权价值= \$行[0]\>$行[0]。< /选项>中;
}
回声< /选择&g​​t;中;
?>
 

我想真正的灵活性,也希望会在上面

相似的另一个脚本使用mysql_field_name动态填充的第一个

I have 2 dropdowns on my HTML page : The first drop down contains the database column names based on which the the second dropdown would be populated i.e.

I have a single table with fields: <Student Name, Degree, City> and following would be the entries;

1. "A", "BS", "New York"
2. "B", "BS", "Chicago"
3. "C", "MS", "Boston"
4. "D", "MS", "New York"

So my first dropdown would contain the column names i.e. "Degree" and "City".

If I select "Degree", the 2nd dropdown should populate "BS" and "MS" and if I select "City", the 2nd dropdown should select "New York", "Boston" and "Chicago".

How can I go about with the implementation?

[Adding my code]:

the changeSecond(first) method remains exactly the same as you suggested

<body>
   <form method="POST" action="" name="mainForm">
      <table>
         <tr>
            <td> Filter by: </td>
            <td>
            <div id="first">
               <select onChange="changeSecond(this.value)">
                  <option value="1">All</option>
                  <option value="2">Degree</option>
                  <option value="3">City</option>
               </select>
            </td>
         </tr>
         <tr>
            <td>&nbsp</td>
            <td>
               <div id="second">
                  <select name="val">
                     <option value=""></option>
                  </select>
               </div>
            </td>
         </tr>
      </table>
   </form>
</body>

And this is the second_script.php as you suggested:

<?
   $link = mysql_connect("localhost", "root", "");

   if (!$link)
   {
      die('Could not connect: ' . mysql_error());
   }
   if (mysql_select_db("myDatabase", $link))
   {
      $first=mysql_real_escape_string($_REQUEST["first"]);
      $query="SELECT ".$first." FROM myTable GROUP BY ".$first;
      $data=mysql_query($query);

      echo "<select id=\"second\">";
      while($row=mysql_fetch_row($data))
      {
         echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
      }
      echo "</select>";
   }
   echo mysql_error();
?>

解决方案

If you want a more dynamic solution (that will accommodate changes to the background DB) you can do something like this on your page:

<script>
        function changeSecond(first){
        var xmlhttp;
        if (window.XMLHttpRequest)
          {// code for IE7+, Firefox, Chrome, Opera, Safari
          xmlhttp=new XMLHttpRequest();
          }
        else
          {// code for IE6, IE5
          xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
          }
        xmlhttp.onreadystatechange=function()
        {
          if (xmlhttp.readyState==4 && xmlhttp.status==200)
            {
            var res=xmlhttp.responseText;
            document.getElementById("second").innerHTML=res;
            }
          }
        xmlhttp.open("GET","second_script.php?first="+first,true);
        xmlhttp.send();
        }
        </script>
...
<select onChange="changeSecond(this.value)">
<option value="Degree">Degree</option>
<option value="City">City</option>
</select>
<div id="second"><select><option value=""></option></select></div>

and then a script similar to:

<?php
//database connection
$first=mysql_real_escape_string($_REQUEST["first"]);
$query="SELECT ".$first." FROM tablename GROUP BY ".$first;
$data=mysql_query($query);
echo "<select>";
while($row=mysql_fetch_row($data)){
echo "<option value=\"".$row[0]."\">".$row[0]."</option>";
}
echo "</select>";
?>

I guess for real flexibility you'd also want to dynamically populate that first one using mysql_field_name in another script similar to above

这篇关于填充下拉 - PHP阿贾克斯MySQL的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆