简体   繁体   English

在PHP中使用标签选择操作

[英]Operations with tag select in PHP

How about, I try to perform a simple operation (addition, subtraction or multiplication). 怎么样,我尝试执行一个简单的操作(加,减或乘)。 99 + 5 = 104 Form 99 + 5 = 104表格

<select> + <input> = result

Between input tags, there is no problem, the problem is when I have a list of numbers to select from the BD. 在输入标签之间没有问题,问题是当我有一个要从BD中选择的数字列表时。

In the BD the following data are available. 在BD中,以下数据可用。 As you can see, the discounts do not match your ID. 如您所见,折扣与您的ID不符。

Id discount
1   100
2   99
3   98
4   96
5   95

If I want to perform an operation, at the time of listing the discounts, the label 如果要执行操作,在列出折扣时,标签

<option value = "<? PHP echo ''. $ row ['id'];?>"> <? PHP echo ''. $ row ['discount'];?> </ option>

The operation is done with the id and not with the discount. 该操作通过id而不是折扣完成。 An example of a sum, we select the discount with 99, then insert the amount of 5 the correct result = 104. 一个总和的示例,我们选择99的折扣,然后将5的金额插入正确的结果= 104。

The problem is that at the time of the operation instead of 99 it takes the value of its ID = 2, consequently 2 + 5 = 7. 问题在于,在操作时,而不是99时,其ID的值= 2,因此2 + 5 = 7。

My question at the time of performing the operation as I can take the discount value instead of Id. 我在执行操作时的问题是,我可以使用折扣值而不是ID。

<html>
<head>
<script language='javascript'>
function fadd(){
 n1=document.f1.cveDiscount.value;
 n2=document.f1.amount.value;
res=document.f1.resul.value;
res=parseInt(n1)+parseInt(n2);
document.f1.resul.value=res;

}
</script>
</head>
<body>
  <form name='f1' action="" method=POST>
  <label name=lblnum1>Num 1:</label>
  <select name="cveDiscount" class="form-control ">
    <?PHP

    if ($resultado = $mysqli->query("SELECT id,discount FROM discount  order by id desc", MYSQLI_USE_RESULT)) {

      while ($row = mysqli_fetch_array($resultado)){
        ?>
        <tr>
          <option value="<?PHP echo ''.$row['id'];?>"><?PHP echo ''.$row['discount'];?></option>
        </tr>
        <?PHP
      }
    }
    ?>
    <option value="#" selected>Select discount</option>
  </select>

  <label name=lblnum2>Num 2:</label>
  <input type=text name='amount'/>

  <input type=button value="   =   " onclick="fadd()">

  <input type=text name='resul'/>

</form>
</body>
</html>

You can use: 您可以使用:

selectedOptions : ...contains a list of the elements contained within the element that are currently selected. selectedOptions :...包含当前选中的元素中包含的元素的列表。 The list of selected options is an HTMLCollection object with one entry per currently selected option. 所选选项的列表是一个HTMLCollection对象,每个当前所选选项都有一个条目。

Hence, you can change from: 因此,您可以从以下位置进行更改:

n1=document.f1.cveDiscount.value;

to: 至:

n1 = document.f1.cveDiscount.selectedOptions[0].textContent;

In any case I'd suggest to use .addEventListener() at dom ready instead to inline event handler. 无论如何,我建议在dom ready时使用.addEventListener()而不是内联事件处理程序。

The snippet: 片段:

 document.addEventListener('DOMContentLoaded', function(e) { document.querySelector('[type=button]').addEventListener('click', function(e) { n1 = document.f1.cveDiscount.selectedOptions[0].textContent; n2 = document.f1.amount.value; res = document.f1.resul.value; res = parseInt(n1) + parseInt(n2); document.f1.resul.value = res; }) }) 
 <form name='f1' action="" method=POST> <label name=lblnum1>Num 1:</label> <select name="cveDiscount" class="form-control "> <option value="1">100</option> <option value="2">99</option> <option value="3">98</option> <option value="4">96</option> <option value="5">95</option> <option value="#" selected>Select discount</option> </select> <label name=lblnum2>Num 2:</label> <input type=text name='amount'/> <input type=button value=" = "> <input type=text name='resul'/> </form> 

Just change value inside option with discount 只需更改带有discount option内的价值

<option value="<?PHP echo ''.$row['discount'];?>"><?PHP echo ''.$row['discount'];?></option>

In this way you can easly pass the discount as value instead of its id 这样,您可以轻松地将discount作为值而不是其id传递

Changing your javascript a bit will also let you use the text from the select tag options, if you need to keep those id values in there for whatever reason: 如果出于某种原因需要将这些id值保留在其中,则稍微更改javascript还可以让您使用select标记选项中的文本:

var sel = document.f1.cveDiscount;
var n1 = sel.options[sel.selectedIndex].text;

This will return the expected results. 这将返回预期的结果。

Currently you're showing the value of Discount in UI but you're actually passing id as the value for the addition. 当前,您正在UI中显示Discount的值,但实际上是传递id作为添加值。 Just retrieve discount from query and put it as value in your option tag as follows: 只需从查询中获取折扣并将其作为值放入您的选项标签中,如下所示:

<select name="cveDiscount" class="form-control ">
    <?PHP

    if ($resultado = $mysqli->query("SELECT discount FROM discount  order by id desc", MYSQLI_USE_RESULT)) {

      while ($row = mysqli_fetch_array($resultado)){
        ?>
        <tr>
          <option value="<?PHP echo ''.$row['discount'];?>"><?PHP echo ''.$row['discount'];?></option>
        </tr>
        <?PHP
      }
    }
    ?>
    <option value="#" selected>Select discount</option>
  </select>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM