2014-02-07 23 views
-1

您好我正在創建一個網站使用HTML,CSS,PHP,MySQL和JavaScript的一點點的jQuery。 此刻我有3個下拉框,動態顯示您的選項,例如,如果我點擊英語,然後下一個下拉框顯示英語的所有部分,如閱讀,然後下一個將涉及閱讀這是所有工作完美。

我想要做的是使用sql查詢中的每個下拉框的值來返回與這些下拉框相關的特定視頻。

我需要爲$ _Post使用什麼類型的數組,以及如何構建sql查詢以便我可以爲視頻提取所有信息。

這是我目前使用的下拉菜單中的代碼

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
    <head> 
<script src="http://code.jquery.com/jquery-1.10.1.min.js"></script> 
<script src="http://code.jquery.com/jquery-migrate-1.2.1.min.js"></script> 
    <script type="text/javascript"> 
     $(document).ready(function(){ 
      $("select#type").attr("disabled","disabled"); 
      $("select#category").change(function(){ 
      $("select#type").attr("disabled","disabled"); 
      $("select#type").html("<option>wait...</option>"); 
      var id = $("select#category option:selected").attr('value'); 
      $.post("select_type.php", {id:id}, function(data){ 
       $("select#type").removeAttr("disabled"); 
       $("select#type").html(data); 
      }); 
     }); 

     $("select#principle").attr("disabled","disabled"); 
      $("select#type").change(function(){ 
      $("select#principle").attr("disabled","disabled"); 
      $("select#principle").html("<option>wait...</option>"); 
      var id = $("select#type option:selected").attr('value'); 
      $.post("select_principle.php", {id:id}, function(data){ 
       $("select#principle").removeAttr("disabled"); 
       $("select#principle").html(data); 
      }); 
     }); 

     $("form#select_form").submit(function(){ 
      var cat = $("select#category option:selected").attr('value'); 
      var type = $("select#type option:selected").attr('value'); 
      var princ = $("select#principle option:selected").attr('value'); 
      if(cat>0 && type>0 && princ>0) 
      { 
       var result = $("select#principle option:selected").html(); 
       $("#result").html('your choice: '+result); 
      } 
      else 
      { 
       $("#result").html("you must choose two options!"); 
      } 
      return false; 
     }); 
    }); 
    </script> 
    </head> 
    <body> 
     <?php include "select.class.php"; ?> 
     <form id="select_form"> 
      Choose a category:<br /> 
      <select id="category"> 
       <?php echo $opt->ShowCategory(); ?> 
      </select> 
     <br /><br /> 

     Choose a type:<br /> 
     <select id="type"> 
      <option value="%">any...</option> 
     </select> 
     <br /><br /> 

     Choose a principle:<br /> 
     <select id="principle"> 
      <option value="%">any...</option> 
     </select> 
     <br /><br /> 

     <input type="submit" value="confirm" /> 
     </form> 
     <div id="result"></div> 
    </body> 
</html> 

這是運行SQL查詢

<?php 
class SelectList 
{ 
    protected $conn; 

     public function __construct() 
     { 
      $this->DbConnect(); 
     } 

     protected function DbConnect() 
     { 
      include "db_config.php"; 
      $this->conn = mysql_connect($host,$user,$password) OR die("Unable to connect to the database"); 
      mysql_select_db($db,$this->conn) OR die("can not select the database $db"); 
      return TRUE; 
     } 

     public function ShowCategory() 
     { 
      $sql = "SELECT * FROM subject"; 
      $res = mysql_query($sql,$this->conn); 
      $category = '<option value="0">choose...</option>'; 
      while($row = mysql_fetch_array($res)) 
      { 
       $category .= '<option value="' . $row['subject_id'] . '">' . $row['description'] . '</option>'; 
      } 
      return $category; 
     } 

     public function ShowType() 
     { 
      $sql = "SELECT * FROM section WHERE subject_id=$_POST[id]"; 
      $res = mysql_query($sql,$this->conn); 
      $type = '<option value="0">choose...</option>'; 
      while($row = mysql_fetch_array($res)) 
      { 
       $type .= '<option value="' . $row['section_id'] . '">' . $row['description'] . '</option>'; 
      } 
      return $type; 
     } 

     public function ShowPrinciple() 
     { 
      $sql = "SELECT * FROM principle WHERE section_id=$_POST[id]"; 
      $res = mysql_query($sql,$this->conn); 
      $principle = '<option value="0">choose...</option>'; 
      while($row = mysql_fetch_array($res)) 
      { 
       $principle .= '<option value="' . $row['principle_id'] . '">' . $row['description'] . '</option>'; 
      } 
      return $principle; 
     } 
} 

$opt = new SelectList(); 
?> 
+0

爲什麼投票下來? – WhySoSerious

+0

我猜有人投票失敗,因爲你沒有提供任何代碼來說明你的問題/問題。 – Brice

+0

1)接受選項,並用'$ _POST'超全局數組訪問它們,2)轉義所有的用戶輸入,3)創建查詢,4)執行它。你究竟遇到了哪個部分? –

回答

0

這裏部分是一些基本的東西:

HTML - 創建界面以收集您的數據

<script type="text/javascript" src="http://code.jquery.com/jquery-1.9.1.js"></script> (put in head) 

<div class='nameinput'>Name: <input type=text /></div> 
<div class='ageinput'>Age: <input type=text /></div> 
<button id = 'clickme'>CLICK</button> 

JS - 收集數據並展示AJAX到PHP

$('#clickme').click(function(){ 
    var username = $('.nameinput').val(); 
    var userage = $('.ageinput').val(); 
    $.ajax({ 
      url: 'nameofphpfile.php', 
     type: POST, 
     data: { name: username, 
       age: userage }, 
    dataType: 'json' 
      }) 
      .done(function(){ alert('Great job!') }) 
      .fail(function(){ alert('The server does not like you!) }); 
           }); 

(注:我是理解的回調,則比較難的部分 - 我已經切換到.done的「新」的形式。失敗,總是)。

PHP - grab the data passed from client with ajax, and put in the db. 

<!php 
$name = $_POST['username']; 
$age = $_POST['userage']; 

$host  = "xxxxx"; 
$user  = "xxxxx"; 
$password = "xxxxx"; 
$dbname  = "xxxxx"; 

$cxn = mysqli_connect($host,$user,$password,$dbname); 
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();} 

$query = " INSERT INTO nameofyourtable 
    (name, age) 
    VALUES 
    ('$name', '$age') " ; 
$result = mysqli_query($cxn, $query) or die ("could not query database 1"); 
?> 

如果你想讀取數據,這種方法是不同的,但這是一個不同的問題。

祝你好運!

我在一年前經歷過這件事,所以我知道你正在經歷什麼。

相關問題