依赖选择框php mysql

时间:2014-09-01 14:42:52

标签: php mysql drop-down-menu

即时新手,我有一个问题是第二个选择不能调用数据,下面是我的数据库结构和代码的表和列。请给我一只手TQVM!

table1 name - movie

id,movieid,moviename

table2 name - movievenue

id,movieid,venueid,mvenue

的index.php

<html>
 <head>
  <script type="text/javascript" src="js/jquery-1.7.1.min.js"></script>
  <script type="text/javascript" src="js/jquery-ui-1.8.17.custom.min.js"></script>

  <script type="text/javascript">
       $(document).ready(function(){


           $("#movie").change(function(){
                 var movie=$("#movie").val();
                 $.ajax({
                    type:"post",
                    url:"venue.php",
                    data:"movie="+movie,
                    success:function(data){
                          $("#venue").html(data);
                    }
                 });
           });
       });
  </script>
</head>
<body>
    Movie :
    <select name="movie" id="movie">
      <option>-select your movie-</option>
    <?php 
    include "db.php"; 
    $result=mysql_query("SELECT movieid, moviename from movie order by moviename");
    while($movie=mysql_fetch_array($result)){

    echo "<option value=$movie[moviename]>$movie[moviename]</option>";

    } ?>
    </select>


    Venue :
    <select name="venue" id="venue">
        <option>-select your venue-</option>
    </select>
 </body>
</html>

venue.php(我希望在第二个选择框中显示&#39;以及它取决于第一个选择ID,这是&#39; movieid&#39;。)

<?php
 include "db.php";

  $movie=$_POST["movie"];
  $result=mysql_query("select movieid,mvenue FROM movievenue where mvenue='$movie' ");
  while($venue=mysql_fetch_array($result)){
   echo"<option value=$venue[mvenue]>$venue[mvenue]</option>";

 }
?>

1 个答案:

答案 0 :(得分:0)

<?php
 include "db.php";

  $movie=$_POST["movie"];
  $result=mysql_query("select movieid,mvenue FROM movievenue where mvenue='{$movie['movieid']}' ");
  while($venue=mysql_fetch_array($result)){
   echo"<option value=$venue[mvenue]>$venue[mvenue]</option>";

 }
?>

另外,在您的第一个文件中,您可能希望选择值为电影的ID

 echo "<option value=$movie[movieid]>$movie[moviename]</option>";