2014-01-07 74 views
0

我正在嘗試使用PHP和Javascript來創建類別選擇器框。 我已經設置好了,這樣Javascript會按照被選擇的順序顯示步驟,並在取消選擇後隱藏。如何從同一頁上的選擇框中獲取值

但是,我不知道如何將選定的選項「ID」或「值」,並將其傳遞到下一行。 (一旦選定的ID或值傳遞,下一個列表可以加載)

這是我的代碼,預先感謝您的期待。如果我做錯了或不正確的方式,請。讓我知道並且/或者告訴我正確的做法。

<?php 
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php"); 
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php"); 
?> 

<div class="content"> 
<form> 
    <select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)"> 
     <?php 

     $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC"); 

     while($row = mysqli_fetch_array($result_c)) 
     { 
      echo '<option class="mso" id="'. $row['category_nameid'] .'"value="'; 
      echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>'; 
     } 

     ?> 
    </select> 

    <select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)"> 
     <?php 

     $var_c = ???? 

     $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC"); 

     while($row = mysqli_fetch_array($result_s)) 
     { 
      echo '<option class="mso" id="'. $row['section_nameid'] .'"value="'; 
      echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>'; 
     } 

     ?> 
    </select> 

    <select name="subsections" class="newcatediv" id="step3" size="3"> 
     <?php 

     $var_s = ???? 

     $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC"); 

     while($row = mysqli_fetch_array($result_ss)) 
     { 
      echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';  
      echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>'; 
     } 

     ?> 
    </select> 

</form> 
</div> 

<?php 
include($_SERVER["DOCUMENT_ROOT"] . "/inc/footer.php"); 
    ?> 

回答

1

默認情況下,在<select>第一個選項被選中,所以這會工作:

<select name="categorys" class="newcatediv" id="step1" size="3" onchange="mine(this.value)"> 
    <?php 

    $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC"); 

    $var_c = null; 
    while($row = mysqli_fetch_array($result_c)) 
    {    
     if($var_c == null) $var_c = $row['category_nameid']; 
     echo '<option class="mso" id="'. $row['category_nameid'] .'"value="'; 
     echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>';    
    } 

    ?> 
</select> 

<select name="sections" class="newcatediv" id="step2" size="3" onchange="mine2(this.value)"> 
    <?php   

    $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC"); 

    $var_s = null; 
    while($row = mysqli_fetch_array($result_s)) 
    { 
     if($var_s == null) $var_s = $row['section_nameid']; 
     echo '<option class="mso" id="'. $row['section_nameid'] .'"value="'; 
     echo $row['section_nameid'] .'">' . $row['section_name'] . '</option>'; 
    } 

    ?> 
</select> 

<select name="subsections" class="newcatediv" id="step3" size="3"> 
    <?php   

    $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC"); 

    while($row = mysqli_fetch_array($result_ss)) 
    { 
     echo '<option class="mso" id="'. $row['subsection_nameid'] .'"value="';  
     echo $row['subsection_nameid'] .'">' . $row['subsection_name'] . '</option>'; 
    } 

    ?> 
</select> 

乾杯

+0

感謝您的評論,但它仍然不加載正確的SQL查詢。它回來了一個空白的聲明。 –

1

嗨:)你不能使用流程,在同一頁PHP的。但你可以用這個jQuery包括3頁來做到這一點。 第一頁:

$(document).ready(function(){ 
$("#step1").change(function(){ 
    var id=$("#step1").val(); 

    alert(id); //shouts the value of the selected step1 
    $.post("select_step2.php", {id:id}, function(data){ 
     $("#step2").empty(); 
     $("#step2").append(data); 

        $("#step2").change(function(){ 
         var id2=$("#step2").val(); 
         alert(id2); //shouts the value of the selected step2 

         $.post("select_step3.php", {id:id2}, function(data){ 
          $("#step3").empty(); 
          $("#step3").append(data); 
          }); 
        }); 
     }); 
    }); 
    }); 

上面的代碼是jQuery的在那裏你可以調用每個數據的依賴於每一個步驟。

<?php 
include($_SERVER["DOCUMENT_ROOT"] . "/inc/header.php"); 
include($_SERVER["DOCUMENT_ROOT"] . "/inc/search.php"); 
?> 


<form> 
First Step: <select name="categorys" class="newcatediv" id="step1" size="3"> 
    <?php 

    $result_c = mysqli_query($con,"SELECT * FROM categories ORDER BY category_name ASC"); 

    while($row = mysqli_fetch_array($result_c)) 
    { 
     echo '<option class="mso" id="'. $row['category_nameid'] .'"value="'; 
     echo $row['category_nameid'] .'">' . $row['category_name'] . '</option>'; 
    } 

    ?> 
</select> 

Second Step: <select name="sections" class="newcatediv" id="step2" size="3"></select> 

Third Step: <select name="subsections" class="newcatediv" id="step3" size="3"></select> 

代碼爲您select_step2.php:

<?php 

//Please include the connection to your database here :) 

$var_c = trim($_POST['id']); 

    $section = ""; 
    $result_s = mysqli_query($con,"SELECT * FROM sections WHERE category_nameid='$var_c' ORDER BY section_name ASC"); 

    while($row = mysqli_fetch_array($result_s)) 
    { 
     $section.="<option value='$row[section_nameid]'>$row[section_name]</option>"; 
    } 
    echo $section; 

?> 

代碼爲您select_step3.php:

<?php 
    //database connection here 
    $var_s = trim($_POST['id']); 

    $subsection= ""; 
    $result_ss = mysqli_query($con,"SELECT * FROM subsections WHERE section_nameid='$var_s' ORDER BY subsection_name ASC"); 

    while($row = mysqli_fetch_array($result_ss)) 
    { 
     $subsection.="<option value='$row[subsection_nameid]'>$row[subsection_name]</option>"; 
    } 
    echo $subsection; 
?> 
相關問題