Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

193
Views
How to disable submit the form when click a another button that belongs to the form

I coded a cart page and used two buttons to decrease and increase the quantity and I make it work correctly using javascript. Half of HTML code is here,

<form action="payment.php" method="post">
<div class="qty__amount">
    <button class="qty__button" id="minus1"><i class="uil uil-minus-square-full"></i></button>
    <input autocomplete="off" class="qtyamount1 ro_form-qty" type="text" id="qtyamount1" name="qty1" value="0" readonly>
    <button class="qty__button" id="plus1"><i class="uil uil-plus-square"></i></button>
</div>

<div class="button">
    <button class="checkout__button" type="submit">Checkout</button>
</div>
</form>

Javascript code is here,

document.getElementById("minus1").onclick = function() {minusButton("qtyamount1","itemPrice1","eachItemTotal1")};                       
document.getElementById("plus1").onclick = function() {plusButton("qtyamount1","itemPrice1","eachItemTotal1")};  

function minusButton(quantityID,itemPriceId,eachItemTotalpriceID){
    priceDecrease(itemPriceId,quantityID);
    quantityDecrease(quantityID);
    eachItemPriceDecrease(eachItemTotalpriceID,itemPriceId);
}
function plusButton(quantityID,itemPriceId,eachItemTotalpriceID){
    priceIncrease(itemPriceId);
    quantityIncrease(quantityID);
    eachItemPriceIncrease(eachItemTotalpriceID,itemPriceId);
}

function quantityDecrease(item){ // quantityDecrease
    
    let quantity = 0;
    quantity = parseInt(document.getElementById(item).value)-1;
    if(quantity<0){}
    else{
    document.getElementById(item).value = quantity.toString();
    }
}

function quantityIncrease(item){ //quantityIncrease
    
    let quantity = 0;
    quantity = parseInt(document.getElementById(item).value)+1;
    console.log(quantity);
    document.getElementById(item).value = quantity.toString();
}

Quantity shows on a read-only input tag because then I can get values using PHP and shows them on another page. So on that page, I implement the code to display that quantity value.

PHP Code is here(from payment.php),

<div class="details">
   <p class="pname">Product Name</p>
   <p class="pvar">Variation</p>
   <p class="pqty">$80.00 &Cross; <?php echo $_POST["qty1"] ?></p>
</div>

So after I added this PHP code to the payment.php file the increase and decrease buttons not working. But it worked before. So I want to know how to fix this automatic submission?

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You just need to add the onsubmit HTML attribute to your form's HTML and call the event.preventDefault function. Like this:

<form onsubmit="event.preventDefault()" action="payment.php" method="post">
    <div class="qty__amount">
      <button class="qty__button" id="minus1" type="button"><i class="uil uil-minus-square-full"></i></button>
      <input autocomplete="off" class="qtyamount1 ro_form-qty" type="text" id="qtyamount1" name="qty1" value="0" readonly>
      <button class="qty__button" id="plus1" type="button"><i class="uil uil-plus-square"></i></button>
    </div>

    <div class="button">
      <button class="checkout__button" type="submit">Checkout</button>
    </div>
</form>
about 4 years ago · Juan Pablo Isaza Report

0

It solved :) I added type="button" to the button.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!