douken1726 2014-10-17 12:14
浏览 18

多选选项onChange with php

I have the following MySql table:

id | type      | mark      | model | series  | year
1    worldwide  NULL         NULL    NULL      NULL
2    1          Acura        NULL    NULL      NULL
3    1          2            NSX     NULL      NULL
4    1          2            3       TunedUp   2000-2003
5    1          2            3       & nbsp    2004-2005

I would like to create a select choice of four stages dependent from one another. For example, choose Sports, the second field select choose Acura, the third NSX and the fourth TunedUP (2000 - 2003) or & nbsp; (2004 - 2005).

I found the following solution with JavaScript ToolBox Dynamic Option List, but it shows me a lot of NULLvalues. I'm looking for a simpler solution. I've tried googling, but I can not even properly describe my problem.

  • 写回答

1条回答 默认 最新

  • doujiayao8433 2014-10-17 12:42
    关注
    /* Create table country */
    CREATE TABLE `country` (
      `id` tinyint(4) NOT NULL auto_increment,
      `country` varchar(20) NOT NULL default '',
      PRIMARY KEY  (`id`)
    )
    
    /*create table state*/
    CREATE TABLE `state` (
      `id` tinyint(4) NOT NULL auto_increment,
      `countryid` tinyint(4) NOT NULL,
      `statename` varchar(40) NOT NULL,
      PRIMARY KEY  (`id`)
    )
    
    /* Create table city */
    CREATE TABLE `city` (
      `id` tinyint(4) NOT NULL auto_increment,
      `city` varchar(50) default NULL,
      `stateid` tinyint(4) default NULL,
      `countryid` tinyint(4) NOT NULL,   PRIMARY KEY  (`id`)
    )
    

    Now to test the demo in working we have to add some records in each of the tables. So run the following queries to add the records.

    /* Inserting records into country table */
    INSERT INTO `country` VALUES (1, 'USA');
    INSERT INTO `country` VALUES (2, 'Canada');
    
    /* Inserting records into state table */
    INSERT INTO `state` VALUES (1, 1, 'New York');
    INSERT INTO `state` VALUES (2, 1, 'Los Angeles');
    INSERT INTO `state` VALUES (3, 2, 'British Columbia');
    INSERT INTO `state` VALUES (4, 2, 'Torentu');
    
    /* Inserting records into city table */
    INSERT INTO `city` VALUES (1, 'Los Angales', 2, 1);
    INSERT INTO `city` VALUES (2, 'New York', 1, 1);
    INSERT INTO `city` VALUES (3, 'Toranto', 4, 2);
    INSERT INTO `city` VALUES (4, 'Vancovour', 3, 2);
    

    Now make one file index.php and paste the below code.

    <form method="post" action="" name="form1"> 
    <center>
    <table width="45%"  cellspacing="0" cellpadding="0">
      <tr>
          <td width="75">Country</td>
          <td width="50">:</td>
          <td  width="150">
          <select name="country" onChange="getState(this.value)">
     <option value="">Select Country</option>
     <?php while ($row=mysql_fetch_array($result)) { ?>
     <option value=<?php echo $row['id']?>><?php echo $row['country']?></option>
     <?php } ?>
         </select>
       </td>
    </tr>
    <tr style="">
        <td>State</td>
        <td width="50">:</td>
        <td >
     <div id="statediv">
            <select name="state" >
            <option>Select State</option>
            </select>
     </div>
        </td>
    </tr>
    <tr>
        <td>City</td>
        <td width="50">:</td>
        <td >
     <div id="citydiv">
              <select name="city">
              <option>Select City</option>
    
               </select>
    
             </div>
    
        </td>
    
      </tr>
      </table>
    </center>
    </form>
    

    In the onChage event of the country drop down we have called getState() function of the javascript. Which change the options values of the State drop down, let’s look at the code the getState() function.

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    
    <script type="text/javascript">
    function getState(countryId) { 
        var strURL="findState.php?country="+countryId;
      var req = getXMLHTTP();
      if (req) {
       req.onreadystatechange = function() {
        if (req.readyState == 4) {
         // only if "OK"
         if (req.status == 200) {      
          document.getElementById('statediv').innerHTML=req.responseText;
          document.getElementById('citydiv').innerHTML='<select name="city">'+
          '<option>Select City</option>'+'</select>';      
         } else {
          alert("Problem while using XMLHTTP:
    " + req.statusText);
         }
        }    
       }   
       req.open("GET", strURL, true);
       req.send(null);
      }  
     }
    <script>
    

    As you can see in the above code we are passing the countryid to the file findState.php, which populate the options in the drop down of the state which is fetched from Ajax , is given below.

    <?php $country=intval($_GET['country']); 
    $con = mysql_connect('localhost', 'root', ''); 
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    mysql_select_db('test');
    $query="SELECT id,statename FROM state WHERE countryid='$country'";
    $result=mysql_query($query);
    
    ?>
    <select name="state" onchange="getCity(<?php echo $country?>,this.value)">
    <option>Select State</option>
    <?php while ($row=mysql_fetch_array($result)) { ?>
    <option value=<?php echo $row['id']?>><?php echo $row['statename']?></option>
    <?php } ?>
    </select>
    

    In the above code for the state dropdown, getCity() function is called on its onChage event having parameters countryId and stateId, now let’s look at the code of the getCity() function

    <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script>
    
    <script type="text/javascript">
         function getCity(countryId,stateId) {   
          var strURL="findCity.php?country="+countryId+"&state="+stateId;
          var req = getXMLHTTP();
    
          if (req) {
    
           req.onreadystatechange = function() {
            if (req.readyState == 4) {
             // only if "OK"
             if (req.status == 200) {      
              document.getElementById('citydiv').innerHTML=req.responseText;      
             } else {
              alert("Problem while using XMLHTTP:
    " + req.statusText);
             }
            }    
           }   
           req.open("GET", strURL, true);
           req.send(null);
          }
    
         }
    

    As you can see in the above ajax function, one file findcity.php is called and this PHP file populate the city dropdown according to the supplied parameters country and state from get method. Now let’s look at the code of findcity.php,

    <?php 
    $countryId = intval($_GET['country']); 
    $stateId   = intval($_GET['state']);
    $con       = mysql_connect('localhost', 'root', ''); 
    
    if (!$con) {
        die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db('test');
    $query="SELECT id,city FROM city WHERE countryid='$countryId' AND stateid='$stateId'";
    $result=mysql_query($query);
    
    ?>
    <select name="city">
    <option>Select City</option>
    <?php while($row=mysql_fetch_array($result)) { ?>
    <option value=<?php echo $row['id']?>><?php echo $row['city']?></option>
    <?php } ?>
    </select>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题