php mysql js 下拉框 二级联动
JS代码
<script type="text/javascript"> function changeappid(){ var appid=document.getElementById("appid").value; <?php $result = TSMLDB::getInstance()->select_sql("select f.appid,t.ftypeid,t.typename from wfappform f join wfformtype t on f.ftypeid=t.ftypeid"); $apptype = array(); while ($row = mysqli_fetch_array($result)): $apptype[] =$row ; endwhile; mysqli_free_result($result); $appcount=count($apptype); ?> document.appform.formtypeid.length = 0; var jsappcount=0; jsapptype = new Array(); jsappcount=<?php echo $appcount;?>; <?php for($j=0;$j<$appcount;$j++) { ?> jsapptype[<?echo $j;?>] = new Array("<?php echo $apptype[$j]['appid'];?>","<?php echo $apptype[$j]['ftypeid'];?>","<?php echo $apptype[$j]['typename'];?>"); <?php } ?> document.appform.formtypeid.options[0] = new Option('Auto Select Type',''); var j; for (j=0;j < jsappcount; j++) { if (jsapptype[j][0] == appid) { document.appform.formtypeid.options[document.appform.formtypeid.length] = new Option(jsapptype[j][2],jsapptype[j][1]); } } } </script>
php代码
form method="post" name="appform" action="<?php echo $_SERVER["PHP_SELF"]?>"> <table border=1 width="400px"> <tr> <td>App</td> <td><?php echo '<select name="appid" id="appid" onchange="changeappid()">'; echo '<option selected="selected" value="">Select App</option>'; $result = TSMLDB::getInstance()->select_wfapp(); while ($row = mysqli_fetch_array($result)): echo "<option value=" . $row["appid"] . ">" . $row["rmk"] . "</option>"; endwhile; mysqli_free_result($result); echo '</select>'; ?></td> </tr> <tr> <td>ftype</td> <td><select name="formtypeid" id="formtypeid"> <option selected="selected" value="">Select Type</option> </select></td> </tr> <tr> <td colspan=2> <input class="tool-buttons" type="submit" id="appsubmit" name="appsubmit" value="StartApp" /> </td> </tr> </table> </form>
参考:
http://www.cnblogs.com/down/archive/2012/05/18/2507683.html
http://developer.51cto.com/art/200912/167998.htm
http://www.okajax.com/a/200812/1202R4H008.html
http://www.jb51.net/article/28904.htm