2014-01-30 26 views
0

如果在下拉列表中選擇的值顯示有關在下拉列表中選擇的值的數據,我有一個下拉列表,現在我搜索框搜索是工作得很好,它顯示的結果沒有刷新頁面,問題是現在它顯示下拉的html表和搜索值結果在同一頁,但我想顯示在同一個html表的搜索結果,看到我的代碼下面,任何人都可以指導我做這個謝謝。搜索MYSQL並使用php和javascript在同一個html表格中顯示結果

<html> 

<select name="client" id="client" style="margin:-8px 0 0 1px;background-color:#E8E8E8;width:104px;position: absolute;"> 
<option value="">Select Client</option> 
<?php 
i am connection to mysql 
$sql=mysql_query("xxxxxxxxxx"); 
$clientid=$_GET['clientid']; 
while($row=mysql_fetch_assoc($sql)) 
{ 
if(strlen($_GET['clientid'])>0 && $_GET['clientid']==$row['clientid']) 
{ 
print' <option id="client" name="client" value="'.$row['clientid'].'">'.$row['clientid'].' </option>'; 
} 
else{ 
print' <option id="client" name="client" value="'.$row['clientid'].'">'.$row['clientid'].' </option>'; 
} 
} 
?> 
</select> 

    <form id="lets_search" action="" style="width:0px;margin:-27px 0 0;text-align:left;"> 
        <input type="text" name="region" id="region"> 
        <input type="text" name="country" id="country"> 
        <input type="submit" value="search" name="search" id="search"> 
      </form> 

    <div id="content"></div> 

    <table id="CPH_GridView1" > 
    <thead class="fixedHeader"> 
    <tr> 
    <th style=" width:103px">Region </th> 
    <th style=" width:102px" >Country </th> 

    <tbody id="fbody" class="fbody" style="width:1660px" > 
    <div id="content"> 
    <?php 
    $client_id = $_POST['title']; 
    if($client_id!=""){ 
    $sql_selectsupplier = "xxxxxxxxxxx"; 
    echo ' <td style="width:103px" class=" '.$rows["net_id"].'">'.$rows["clientid"].'</td> 
      <td style="width:102px" id="CPH_GridView1_clientid" class=" '.$rows["net_id"].'">'.$rows["region"].'</td>'; 

    </div> 
    </tbody> 
    </table> 

    </html> 

//javascript on the same page 

<script type="text/javascript"> 
     $(function() { 
     $("#lets_search").bind('submit',function() { 
     var valueregion = $('#region').val(); 
      var valuecountry = $('#country').val(); 
      $.post('clientnetworkpricelist/testdb_query.php',{valueregion:valueregion,valuecountry:valuecountry}, function(data){ 
      $("#content").html(data); 
      }); 
      return false; 
     }); 
     }); 
    </script> 

testdb_query.php

<?php 

$dbHost = 'localhost'; // usually localhost 
$dbUsername = 'xxxxxx'; 
$dbPassword = 'xxxxxxxxxxxx'; 
$dbDatabase = 'xxxxxxxxxxxxxx'; 
$db = mysql_connect($dbHost, $dbUsername, $dbPassword) or die ("Unable to connect to Database Server."); 
mysql_select_db ($dbDatabase, $db) or die ("Could not select database."); 

$region=$_POST['valueregion']; 
$country=$_POST['valuecountry']; 
$clientid=$_POST['clientid']; 

if (strlen($region) > 0 && $region!=""){ 

       $sql_search.= " AND s.region = '".$region."' "; 

       } 

if (strlen($country) > 0 && $country!=""){ 

       $sql_search.= " AND s.country = '".$country."' "; 

       } 
$query = mysql_query("SELECT * FROM supplierprice s,$clientid c WHERE s.supp_price_id = c.net_id $sql_search"); 

echo '<table>'; 

while ($data = mysql_fetch_array($query)) { 

    echo ' 
    <tr> 
     <td style="font-size:18px;">'.$data["region"].'</td>   
    <td style="font-size:18px;">'.$data["country"].'</td> 
</tr>'; 

} 

echo '</table>'; 
?> 

回答

0

好吧,我看到你的HTML代碼的兩個問題。一個是你使用兩個具有相同ID(「content」)的html元素,這不是ID的用途。其次,在tbody中放置div不是有效的HTML。

從你的解釋我得知,你試圖在一個表中顯示兩個行爲的結果。 所以,從代碼和更新代碼刪除第一個div

<div id="content"></div> 

內$。員額,以這樣的

$("#CPH_GridView1").append(data); 

此外,刪除裏面TBODY股利爲好。

1

最佳實踐將php代碼與html分開 - 在呈現html之前從數組中獲取數據庫中的所有數據,然後使用foreach在html中解析每行的afetr。

把DB登錄和連接在目前存在的文件,並與require_once()在頁面的頂部inlcude它understandig腳本的更好

顯示錯誤

ini_set('display_errors',1); 
ini_set('display_startup_errors',1); 
error_reporting(-1); 

評論「我是連接到MySQL」這一行,因爲它會帶來一個錯誤在這種格式

連接到數據庫後初始化

$sql_search = ""; // otehrwise it will bring a notice when calling "$sql_search.=" 

,並檢查HTTP_REQUEST所以它不會帶來任何錯誤,請首先訪問頁面時沒有$ _ POST數據

if ($_SERVER['REQUEST_METHOD'] === 'POST') 
{ 
    //code for displaying the new table with the post data 
} 
相關問題