根据下拉列表获取值

发布于 2024-10-21 09:57:30 字数 1404 浏览 3 评论 0原文

我有一个项目的下拉列表。我想要做的是获取商品数量、剩余数量和已发货商品值,这取决于下拉列表中的值。

我有这个代码:

<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname">
<?php
  $item="SELECT item_name FROM stocks";
  $itemresult = @mysql_query($item)or die ("Error in query: $query. " .                                     mysql_error());
  while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><?php 
$row = mysql_fetch_object($itemresult);

echo $row->item_quantity; ?></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><?php echo $row->rem_quantity; ?></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><?php echo $row->stocks_dispatched; ?></td></tr>
<tr>
<td>Add Stocks:</td>
<td><input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td>&nbsp;</td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>

我真的不知道如何开始代码,因为我是这种方法的新手。如果您能帮助我,我将不胜感激。提前致谢。

I have a dropdown list for items. What I want to do is get the item quantity, remaining quantity and dispatched item values which depends on the value in the drop down.

I have this code :

<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname">
<?php
  $item="SELECT item_name FROM stocks";
  $itemresult = @mysql_query($item)or die ("Error in query: $query. " .                                     mysql_error());
  while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><?php 
$row = mysql_fetch_object($itemresult);

echo $row->item_quantity; ?></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><?php echo $row->rem_quantity; ?></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><?php echo $row->stocks_dispatched; ?></td></tr>
<tr>
<td>Add Stocks:</td>
<td><input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>

I really don't know how to start the code because i'm new to this approach. If you can help me it's much appreciated. Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

负佳期 2024-10-28 09:57:30

这是有效的:要点是页面使用 ajax 调用脚本,该脚本将传递您需要的值的 XML,然后相应地填充这些字段。 :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script>
function updateFields(itemname){
    var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var xmlDoc=xmlhttp.responseXML;
    var iQdata=xmlDoc.documentElement.getElementsByTagName("ITEMQUANTITY");
    var rQdata=xmlDoc.documentElement.getElementsByTagName("REMAININGQUANTITY");
    var sDdata=xmlDoc.documentElement.getElementsByTagName("STOCKSDISPATCHED");
    var iQ=iQdata[0].firstChild.nodeValue;
    var rQ=rQdata[0].firstChild.nodeValue;
    var sD=sDdata[0].firstChild.nodeValue;
    document.getElementById("itemQuantity").innerHTML=iQ;
    document.getElementById("remainingQuantity").innerHTML=rQ;
    document.getElementById("stocksDispatched").innerHTML=sD;
    }
  }
xmlhttp.open("GET","quantity_script.php?itemname="+itemname,true);
xmlhttp.send();

}
</script>

<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname" onchange="updateFields(this.value)">
<?php
  $item="SELECT item_name FROM stocks";
  $itemresult = @mysql_query($item)or die ("Error in query: $query. " .mysql_error());
  while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><div id="itemQuantity"></div></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><div id="remainingQuantity"></div></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><div id="stocksDispatched"></div></td></tr>
<tr>
<td>Add Stocks:</td>
<td><input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>

</body>
</html>

然后您还可以在您的网站“quantity_script.php”上创建一个脚本,如下所示:

<?php
//include your DB connection info
$itemname=mysql_real_escape_string($_POST["itemname"]);
$item="SELECT ".$itemname." FROM stocks";
$itemresult = mysql_query($item);
$row=mysql_fetch_assoc($itemresult);
header('Content-type: text/xml');
echo "<ROOT>";
echo "<ITEMQUANTITY>".$row["item_quantity"]."</ITEMQUANTITY>";
echo "<REMAININGQUANTITY>".$row["rem_quantity"]."</REMAININGQUANTITY>";
echo "<STOCKSDISPATCHED>".$row["stocks_dispatched"]."</STOCKSDISPATCHED>";
echo "</ROOT>";
?>

This works: The jist is that the page uses ajax to call a script, which will deliver XML of the values you need, and will then populate those fields accordingly. :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<script>
function updateFields(itemname){
    var xmlhttp;
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.onreadystatechange=function()
{
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    var xmlDoc=xmlhttp.responseXML;
    var iQdata=xmlDoc.documentElement.getElementsByTagName("ITEMQUANTITY");
    var rQdata=xmlDoc.documentElement.getElementsByTagName("REMAININGQUANTITY");
    var sDdata=xmlDoc.documentElement.getElementsByTagName("STOCKSDISPATCHED");
    var iQ=iQdata[0].firstChild.nodeValue;
    var rQ=rQdata[0].firstChild.nodeValue;
    var sD=sDdata[0].firstChild.nodeValue;
    document.getElementById("itemQuantity").innerHTML=iQ;
    document.getElementById("remainingQuantity").innerHTML=rQ;
    document.getElementById("stocksDispatched").innerHTML=sD;
    }
  }
xmlhttp.open("GET","quantity_script.php?itemname="+itemname,true);
xmlhttp.send();

}
</script>

<form method="post" action="restore_stocks.php">
<table>
<tr>
<td>Item Name:</td>
<td><select name="itemname" onchange="updateFields(this.value)">
<?php
  $item="SELECT item_name FROM stocks";
  $itemresult = @mysql_query($item)or die ("Error in query: $query. " .mysql_error());
  while($row=@mysql_fetch_array($itemresult)){
echo "<OPTION VALUE=".$row['item_name'].">".$row['item_name']."</option>";
}
?>
</select></td></tr>
<tr>
<td>Item Quantity:</td>
<td><div id="itemQuantity"></div></td></tr>
<tr>
<td>Remaining Quantity:</td>
<td><div id="remainingQuantity"></div></td></tr>
<tr>
<td>Stocks Dispatched:</td>
<td><div id="stocksDispatched"></div></td></tr>
<tr>
<td>Add Stocks:</td>
<td><input name="addstocks" type="text" size="25" maxlength="25" /></td></tr>
<tr>
<td> </td>
<td><input type="submit" name="Submit" id="Submit" value="Restore" /></td>
</tr>
</table>
</form>

</body>
</html>

And then you'd also make a script on your site "quantity_script.php" that would be something like the following:

<?php
//include your DB connection info
$itemname=mysql_real_escape_string($_POST["itemname"]);
$item="SELECT ".$itemname." FROM stocks";
$itemresult = mysql_query($item);
$row=mysql_fetch_assoc($itemresult);
header('Content-type: text/xml');
echo "<ROOT>";
echo "<ITEMQUANTITY>".$row["item_quantity"]."</ITEMQUANTITY>";
echo "<REMAININGQUANTITY>".$row["rem_quantity"]."</REMAININGQUANTITY>";
echo "<STOCKSDISPATCHED>".$row["stocks_dispatched"]."</STOCKSDISPATCHED>";
echo "</ROOT>";
?>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文