2012-01-10 128 views
0

我需要一些幫助,請,MySQL數據 - 下拉列表

我有一個PHP頁面下拉列表,通過MySQL數據庫查詢填充。 我希望能夠顯示其他表格單元格中所選選項的數據庫詳細信息。理想情況下,這可以在沒有頁面刷新的情況下實現。

除了這個表將包括多達75行的(貨盤上的車輛 - 這是一個銷售工具),所以需要有一個while語句或東西達致這。每行將有一個選擇框來選擇一個packcode。

我的代碼與下拉列表如下,該表只包含5行現在。

我知道我需要使用ajax或JavaScript除此之外?

如果任何人有一個示例腳本或可以使用我的代碼作爲例子,我真的很感激它。

<? 

    $con = mysql_connect("localhost","user","password"); 
    if (!$con) 
    { 
    die('Could not connect: ' . mysql_error()); 
    } 

    mysql_select_db("dbname", $con); 

    $packcodesql="SELECT packcode from skudata order by packcode"; 
    $resultpackcode=mysql_query($packcodesql); 
    $optionspackcode=""; 

    while ($row=mysql_fetch_array($resultpackcode)) { 
    $packcode=$row["packcode"]; 
    $optionspackcode.="<OPTION VALUE=\"$packcode\">".$packcode; 
    } 
?> 

<table border=1> 
<tr> 
    <td>Pack Code</td> 
    <td>Category</td> 
    <td>Selling Units</td>   
    <td>Full Pallet QTY</td> 
    <td>Order QTY</td> 
</tr> 
<Tr> 
    <td> 
    <SELECT NAME=packcode1 style="width:100px;"> 
     <OPTION VALUE=0><?=$optionspackcode?></SELECT> 
    </td> 
    <td> 
    <!-- show mysql result for "select Category from skudata where packcode=packcode1" --> 
    </td> 
    <td> 
    <!-- show mysql result for "select SellingUnits from skudata where packcode=packcode1" --> 
    </td>  
    <td> 
    <!-- show mysql result for "select FullPalletQTY from skudata where packcode=packcode1" --> 
    </td> 
    <td><input type="text" id="qty" name="qty"></td> 
</tr>  
<Tr> 
    <td> 
    <SELECT NAME=packcode2 style="width:100px;"> 
     <OPTION VALUE=0><?=$optionspackcode?></SELECT> 
    </td> 
    <td> 
    <!-- show mysql result for "select Category from skudata where packcode=packcode2" --> 
    </td> 
    <td> 
    <!-- show mysql result for "select SellingUnits from skudata where packcode=packcode2" --> 
    </td>  
    <td> 
    <!-- show mysql result for "select FullPalletQTY from skudata where packcode=packcode2" --> 
    </td> 
    <td><input type="text" id="qty" name="qty"></td> 
</tr>  
<Tr> 
    <td> 
    <SELECT NAME=packcode3 style="width:100px;"> 
     <OPTION VALUE=0><?=$optionspackcode?></SELECT> 
    </td> 
    <td> 
    <!-- show mysql result for "select Category from skudata where packcode=packcode3" --> 
    </td> 
    <td> 
    <!-- show mysql result for "select SellingUnits from skudata where packcode=packcode3" --> 
    </td>   
    <td> 
    <!-- show mysql result for "select FullPalletQTY from skudata where packcode=packcode3" --> 
    </td> 
    <td><input type="text" id="qty" name="qty"></td> 
</tr>  
<Tr> 
    <td> 
    <SELECT NAME=packcode4 style="width:100px;"> 
     <OPTION VALUE=0><?=$optionspackcode?></SELECT> 
    </td> 
    <td> 
    <!-- show mysql result for "select Category from skudata where packcode=packcode4" --> 
    </td> 
    <td> 
    <!-- show mysql result for "select SellingUnits from skudata where packcode=packcode4" --> 
    </td>   
    <td> 
    <!-- show mysql result for "select FullPalletQTY from skudata where packcode=packcode4" --> 
    </td> 
    <td><input type="text" id="qty" name="qty"></td> 
</tr>  
</table> 

回答

2

你想填充與數據庫中的數據鏈接到TD上方的框?

如果是這樣,你可以使用AJAX,是的,在選擇框的選項放在一個onclick(相當肯定,應該這樣做)。

<select> 
    <option onclick="myAjaxFunction(this);">Some name</option> 
    <option onclick="myAjaxFunction(this);">Some other name</option> 
</select> 

,那麼你就必須創建函數myAjaxFunction其中將包含您的代碼Ajax請求(http://api.jquery.com/jQuery.ajax/)。 簡單的例子是:

<script> 
function myAjaxFunction(elem) { 
    $.ajax({ 
     url: 'target/file.php', 
     success: function(response) { 
      $("#target-td").html(response); 
     } 
    }); 
} 
</script> 

最後,你調用與AJAX,包含你的數據庫調用一個PHP文件。在文件中,您只需回顯您想要顯示的內容。

理想情況下,您將執行一次調用並使用json將其全部返回。一個屬性

dataType: 'json' 

可以被添加到$就()調用,並且可以使用:

echo json_encode($myContent); 
在PHP

你JSON編碼該php含量(優選在陣列中())。

這應該都指出你的方式:)請告訴我,如果我需要更具體的,或提供更好的例子...

UPDATE

您可以創建一個唯一的ID對每個你想要定位的td的。然後創建

<td> 
    <select> 
     <option onclick="firstPackCodeAjax('<?=$packcodeValue?>');" value="<?=$packcodeValue?>"><?=$packcodeValue?></option> 
    </select> 
</td> 
<td id="categoryTd"> 
    <!-- show mysql result for "select Category from skudata where packcode=packcode1" --> 
</td> 
<td id="unitsTd"> 
    <!-- show mysql result for "select SellingUnits from skudata where packcode=packcode1" --> 
</td>  
<td id="palletTd"> 
    <!-- show mysql result for "select FullPalletQTY from skudata where packcode=packcode1" --> 
</td> 

那麼你的AJAX功能將是:

<script> 
function firstPackCodeAjax(packCode) { 
    $.ajax({ 
     url: 'target/file.php', 
     data: {code: packCode}, 
     dataType: 'json', 
     success: function(json) { 
      $("#categoryTd").html(json.Category); 
      $("#unitsTd").html(json.SellingUnits); 
      $("#palletTd").html(json.FullPalletQTY); 
     } 
    }); 
} 
</script> 

這預計將數據輸出爲JSON和格式:

[ 
    { "Category": "Fast cars" }, 
    { "SellingUnits": "9001" }, 
    { "FullPalletQTY": "9001" } 
]; 

你會再創建一個函數爲每個選擇你想拉AJAX中。 target/file.php你需要在某處創建自己。在這裏你獲取數據並在json中回顯出來。祝你好運;)此外,這可以很容易地優化,但多數民衆贊成在以後...

+0

嗨,感謝您花時間回覆。這是我與Ajax的第一次交互,所以如果你能提供更好,更完整的例子,我會很感激。 – Smudger 2012-01-10 09:57:26

+0

當我下班回家時(大約4小時),我們會嘗試擴展這些例子。 與此同時,我建議讀一點關於AJAX;)http://net.tutsplus.com/tutorials/javascript-ajax/5-ways-to-make-ajax-calls-with-jquery/ – Tehnix 2012-01-10 10:10:25

+0

肯定地, 謝謝,瑞安 – Smudger 2012-01-10 10:39:07

1

您可以爲此使用AJAX。我沒有任何示例可以使用,但關於結合AJAX和PHP的w3schools教程是一個很好的起點。 http://w3schools.com/ajax/ajax_aspphp.asp我會修改他們的示例,使用與w3schools網站上的類似的JavaScript函數,將所有選項標記值直接輸出到您的選擇標記中。

希望這會有所幫助!

+0

謝謝,我正試圖現在在PHP學校,在一個PHP的例子。謝謝, Ryan – Smudger 2012-01-10 09:58:00