2016-03-04 126 views
2

我想diplay我的表,當我點擊下拉列表中值。當我查看頁面代碼源時,表值存在但未顯示。我想從mysql中使用下拉列表中的值進行查詢,並使用mysql查詢中的數據填充表。的Html下拉菜單顯示什麼

這裏是我的代碼:

<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet"href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 

<script type="text/javascript"> 
    function jsFunction(value){ 
    <?php 
     $con=mysqli_connect("localhost","root","","dengue"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 
     $result = mysqli_query($con,"SELECT * FROM mapping_details where case_type='value'"; 
     echo '<div class="table-responsive ">   
     <table class="table table-condensed table-hover table-bordered"> 
     <thead> 
    <tr> 
    <th>Barangay</th> 
    <th>Case Type</th> 
    <th>Total Dengue Cases</th>  
    </tr> 
</thead> 
<tbody>'; 
    while($row = mysqli_fetch_array($result)) 
    { 
    echo "<tr>"; 
    echo "<td>" . $row['brgy'] . "</td>"; 
    echo "<td>" . $row['case_type'] . "</td>"; 
    echo "<td>" . $row['total_case_brgy'] . "</td>"; 

    echo "</tr>"; 
    } 
echo '</tbody></table></div>'; 
mysqli_close($con); 
?> 
} 
</script> 
    </head> 
    <body> 
     <select id ="ddl" name="ddl" onchange="jsFunction(this.value);"> 
     <option value='1'>One</option> 
     <option value='2'>Two</option> 
     <option value='3'>Three</option> 
    </select> 
    </body> 
    </html> 

原來,在我的其他網頁,PHP代碼爲人體內部。但是這一次,如果我使用下拉菜單,我不能使它工作,這就是爲什麼我把它放在函數內部,以便能夠使用onchange函數。我已經閱讀了一些使用AJAX或JSON的答案,但我並不熟悉這些,所以如果可能的話,我只想使用JavaScript,因爲它可能是最簡單的方法。

+0

你的PHP代碼使用回聲'script'標籤裏面......我想這應該是*不*內部'腳本「標籤並移動到」body「。 – Mottie

+0

嗨@Mottie。謝謝你指出我的錯誤。 Jon Foley的回答下面會產生錯誤並且什麼都不會查詢。你能告訴我如何以正確的方式執行此操作嗎? – CretienSC

+0

對不起,我不知道PHP足以幫助;但它聽起來像查詢代碼需要重新檢查。 – Mottie

回答

0

你正在嘗試做的,是在瀏覽器中,你不能做運行PHP代碼。所有的HTML,CSS和JavaScript都發送到客戶端(您的瀏覽器),然後由瀏覽器呈現並初始化。當你打電話給你onchange事件調用jsFunction()你正在嘗試在該函數內執行PHP但是 - 你是在Chrome或Firefox或一些瀏覽器中運行的客戶端,你不能執行PHP那裏。 PHP必須在頁面加載時運行,然後可以在發送到客戶端之前更改html,css或JavaScript。

有做你想做的事情有兩種方式。您可以發送一個Ajax請求(調用你的服務器使用JavaScript)來獲取新的數據,也可以前提交重新加載頁面的表單和你一樣是舒適與 - 修改HTML,CSS可以運行PHP,和/或javascript被返回給客戶端(瀏覽器)。

這裏是一個只是不斷重新加載頁面的非Ajax方式

<!DOCTYPE html> 
<html> 
<head> 
    <meta name="viewport" content="width=device-width, initial-scale=1"> 
    <link rel="stylesheet"href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css"> 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script> 
    <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"></script> 

    <?php 
     $value = isset($_POST['ddl']) ? $_POST['dll'] : 1; // we are setting 1 as the default 
     $con=mysqli_connect("localhost","root","","dengue"); 
     // Check connection 
     if (mysqli_connect_errno()) 
     { 
     echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
     } 
     $result = mysqli_query($con,"SELECT * FROM mapping_details where case_type='{$value}'"; 
     $html= '<div class="table-responsive ">   
     <table class="table table-condensed table-hover table-bordered"> 
     <thead> 
    <tr> 
    <th>Barangay</th> 
    <th>Case Type</th> 
    <th>Total Dengue Cases</th>  
    </tr> 
</thead> 
<tbody>'; 
    while($row = mysqli_fetch_array($result)) 
    { 
    $html.= "<tr>"; 
    $html.= "<td>" . $row['brgy'] . "</td>"; 
    $html.= "<td>" . $row['case_type'] . "</td>"; 
    $html.= "<td>" . $row['total_case_brgy'] . "</td>"; 

    $html.= "</tr>"; 
    } 
$html.= '</tbody></table></div>'; 
mysqli_close($con); 
?> 
} 
</script> 
    </head> 
    <body> 
     <form method="POST" action=""> 
     <select id="ddl" name="ddl"> 
      <option value='1'>One</option> 
      <option value='2'>Two</option> 
      <option value='3'>Three</option> 
     </select> 
     </form> 

     <script> 
     $("select#ddl").on("change",function(){ 
      $('form').submit(); 
     }); 
     </script> 

     <?php echo $html; ?> 

    </body> 
    </html> 
+0

我得到這個錯誤: '通知:未定義指數:DLL在C:\ XAMPP \ htdocs中\ Newfolder \ testing.php第10行'。第二件事是,它什麼也沒有返回,我只能看到表頭,但沒有任何價值,所以也許它什麼都不問。謝謝。 – CretienSC

+0

我已解決問題。現在代碼是測試代碼。它的運行良好。 –

+0

現在代碼更有意義。希望它能幫助你。 –