简体   繁体   中英

Operations with tag select in PHP

How about, I try to perform a simple operation (addition, subtraction or multiplication). 99 + 5 = 104 Form

<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.

In the BD the following data are available. As you can see, the discounts do not match your 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. An example of a sum, we select the discount with 99, then insert the amount of 5 the correct result = 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.

My question at the time of performing the operation as I can take the discount value instead of 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. The list of selected options is an HTMLCollection object with one entry per currently selected option.

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.

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

<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

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:

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. 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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