2012-01-10 123 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