,我需要一些帮助
我有一个PHP页面,其中有一个下拉列表,由mysql数据库查询填充。我希望能够在其他表格单元格中显示所选选项的数据库详细信息。理想情况下,这可以在不刷新页面的情况下实现。
除此之外,该表将由多达75行(车辆上的托盘-这是用于销售工具的)组成,因此需要用while语句或其他内容来实现这一点。每一行都有一个选择框来选择一个分组代码。
下面是我的下拉列表代码,该表目前只包含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>
您想用链接到td上方方框的数据库中的数据填充吗?
如果是这样的话,您可以使用AJAX耶,并在选择框的选项上单击(非常确定应该这样做)。
<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'
可以添加到$.ajax()调用中,并且可以使用:
echo json_encode($myContent);
在PHP中,您对PHP内容进行json编码(最好是在array()中)。
这本应该为你指明方向:)如果我需要更具体,或者提供更好的例子,请告诉我。。。
更新
您可以为每个要作为目标的td创建唯一的id。然后创建
<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中进行回显。祝你好运;)此外,这可以很容易地进行优化,但这是为了以后。。。
您可以使用AJAX来实现这一点。我手头没有任何例子,但w3schools关于AJAX和PHP结合的教程是一个很好的起点。http://w3schools.com/ajax/ajax_aspphp.asp我会修改他们的例子,使用与w3schools网站上的javascript函数类似的javascript函数,将所有选项标记值输出到select标记中。
希望这能有所帮助!