Javascript价格扣除问题

时间:2018-04-30 02:26:29

标签: javascript html

所以我试图为我的交易页面创建一个系统,这样当用户购买10张票时,他的总票数将是70,但是我需要它,所以之后的每张票都以正常价格出售。这是我到目前为止的地方,它适用于一些例子(例如5名成人7名学生),但不适用于其他人(9和2)

如果有人能找到我出错的地方以及为什么有些数字有效而其他人不知道,我将永远感激不尽。是的,我知道我的代码有点草率我提前道歉,但谢谢你,如果你可以帮忙!

<form id="newshow" action="required/post/saveTransaction.php" method="post" oninput="
bothTickets.value = Math.round(ticketOne.value) + Math.round(ticketTwo.value); 
var basePrice;
var TempAmt;
var newTicketValueOne;
var newTicketValueTwo;
if (bothTickets.value == 10) {
    basePrice = 70
    ticketOnePrice = 0
    ticketTwoPrice = 0
    calculation.value = Math.round(ticketOne.value * ticketOnePrice) + Math.round(ticketTwo.value * ticketTwoPrice) + basePrice;
} else if (bothTickets.value >= 10) {
    if (ticketOne.value >= 10) {
        newTicketValueOne = Math.round(ticketOne.value) - 10;
        newTicketValueTwo = Math.round(ticketTwo.value);
    } else if (ticketTwo.value >= 10) {
        newTicketValueTwo = Math.round(ticketTwo.value) - 10;
        newTicketValueOne = Math.round(ticketOne.value);
    } else if (ticketOne.value >= 5) {
        newTicketValueOne = Math.round(ticketOne.value) - 5;
        newTicketValueTwo = Math.round(ticketTwo.value) - 5;
    } else if (ticketTwo.value >= 5) {
        newTicketValueTwo = Math.round(ticketTwo.value) - 5;
        newTicketValueOne = Math.round(ticketOne.value) - 5;
    } else {
        newTicketValueOne = Math.round(ticketOne.value);
        newTicketValueTwo = Math.round(ticketTwo.value);
    }


    basePrice = 70
    ticketOnePrice = 10
    ticketTwoPrice = 7
    tempAmt =  basePrice + Math.round(newTicketValueOne * ticketOnePrice) + Math.round(newTicketValueTwo * ticketTwoPrice);
    calculation.value = tempAmt

} else {
    basePrice = 0
    ticketOnePrice = 10
    ticketTwoPrice = 7
    calculation.value = Math.round(ticketOne.value * ticketOnePrice) + Math.round(ticketTwo.value * ticketTwoPrice);
}

base.value = basePrice;
//calculation.value = Math.round(ticketOne.value * ticketOnePrice) + Math.round(ticketTwo.value * ticketTwoPrice);
totalAmount.value = Math.round(calculation.value); 
changeDue.value = Math.round(moneyGiven.value - totalAmount.value);">
    <fieldset>

    <input type="hidden" name="teamID" value="<?php echo $_SESSION['teamID']; ?>" />
    <h4>Amount of Adults</h4>
        <input class="form-control" name="ticketOne" type="number" placeholder="0">
    <br />
    <h4>Amount of Students</h4>
        <input class="form-control" name="ticketTwo" type="number" placeholder="0">
    <br />
    <h4>Money Owed</h4>
        <input class="form-control" name="totalAmount" readonly="1">
    <br />
        <h4>Money Given</h4>
        <input class="form-control" name="moneyGiven" type="number" required>
        <h4>Change due</h4>
        <input class="form-control" name="changeDue" readonly="1">
        <input type="hidden" name="username" value="<?php echo $_SESSION['username']; ?>" />
        <input type="hidden" class="form-control" name="bothTickets" readonly="1">
        <input type="hidden" class="form-control" name="base" readonly="1">
        <input type="hidden" class="form-control" name="calculation" readonly="1">
    </fieldset>

    <div class="modal-footer">
            <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
             <input type="submit" class="btn btn-primary" value="Process"/>
          </div>
      </form>

1 个答案:

答案 0 :(得分:0)

一条建议:不要让服务器信任客户端提供的值,您需要重新计算服务器上的金额和更改,因为任何用户都可以将任何值发布到服务器。

您可以执行以下操作:

var adultTickets = 0,studentTickets=0,monyGiven=0,moneyOwed=0,change=0;
const calculateValues = () => {
  if(adultTickets+studentTickets>=10){//discount, more than 10 tickets
    const adultLeft = adultTickets-10;//how many of the tickets are adult tickets
    if(adultLeft>0){//more than 10 are adult tickets
      moneyOwed = 70 + (adultLeft*10) + (studentTickets*7);
    }else{//less than 10 are adult tickets
      moneyOwed = 70 + ((studentTickets+adultLeft)*7);
    }
  }else{
    moneyOwed = (adultTickets*10) + (studentTickets*7);
  }
  change = monyGiven-moneyOwed;
}
const setHtml = () => {
  document.querySelector(`[name="totalAmount"]`).value=moneyOwed;
  if(!(change<0)){
    document.querySelector(`[name="changeDue"]`).value=change;
  }
}
const changeAdultTicket = howMany => {
  adultTickets = Math.floor(Number(howMany));
  calculateValues();
  setHtml();
}
const changeStudentTicket = howMany => {
  studentTickets = Math.floor(Number(howMany));
  calculateValues();
  setHtml();
}
const changeMonyGiven = howMany => {
  monyGiven = Math.floor(Number(howMany));
  calculateValues();
  setHtml();
}
document.getElementById("newshow").addEventListener(
  "input",
  (e)=>{
    const inputName = e.target.getAttribute("name");
    if(inputName==="ticketOne"){
      changeAdultTicket(e.target.value);
    }
    if(inputName==="ticketTwo"){
      changeStudentTicket(e.target.value);
    }
    if(inputName==="moneyGiven"){
      changeMonyGiven(e.target.value);
    }

  }
)
<form id="newshow" action="required/post/saveTransaction.php" method="post">
  <fieldset>

    <input type="hidden" name="teamID" value="<?php echo $_SESSION['teamID']; ?>" />
    <h4>Amount of Adults</h4>
    <input class="form-control" name="ticketOne" type="number" placeholder="0" step="1">
    <br />
    <h4>Amount of Students</h4>
    <input class="form-control" name="ticketTwo" type="number" placeholder="0" step="1">
    <br />
    <h4>Money Owed</h4>
    <input class="form-control" name="totalAmount" readonly="1">
    <br />
    <h4>Money Given</h4>
    <input class="form-control" name="moneyGiven" type="number" required>
    <h4>Change due</h4>
    <input class="form-control" name="changeDue" readonly="1">
    <input type="hidden" name="username" value="<?php echo $_SESSION['username']; ?>" />
    <input type="hidden" class="form-control" name="bothTickets" readonly="1">
    <input type="hidden" class="form-control" name="base" readonly="1">
    <input type="hidden" class="form-control" name="calculation" readonly="1">
  </fieldset>

  <div class="modal-footer">
    <button class="btn btn-default" type="button" data-dismiss="modal">Close</button>
    <input type="submit" class="btn btn-primary" value="Process" />
  </div>
</form>