简体   繁体   English

PHP 单击任何按钮时提交表单

[英]PHP form submitting when clicking any buttons

currently setting up a form to create a new order within a payment system and when I click any of the buttons within the form the form submits.当前设置表单以在支付系统中创建新订单,当我单击表单中的任何按钮时,表单提交。 Anyone know what the issue could be?有谁知道可能是什么问题? This is the code with the issue:这是有问题的代码:

 <form action="scripts/php/addorder.php" method="POST"> <script src="scripts/javascript/cookies.js"></script> <script src="scripts/javascript/addproduct.js"></script> <label>Choose Customer: </label> <input type="text" value="" name="name" id="name" required readonly> <button onclick ="window.open('customertable.php','popup','width=600,height=600');">Choose</button> <br/> <div id="products"> <label>Products: </label> <br/> ID: <input type="text" value=" " name="chosenproduct0" id="chosenproduct0" required readonly> Name: <input type="text" value=" " name="chosenproduct0name" id="chosenproduct0name" required readonly> Price: <input type="text" value=" " name="chosenproduct0price" id="chosenproduct0price" required readonly> Quantity: <input type="number" value="1" name="chosenproduct0quantity" id="chosenproduct0quantity" required> <button onclick ="findproduct('chosenproduct0');">Choose Product</button> </div> <br/> <label>Discount: </label> <input type="number" placeholder="0" name="discount" id="discount" step=".01" min="0"> <label>Total: </label> <input type="number" value="0" name="total" id="total" readonly> <button onclick="addproduct()">Add Product</button> <br/> <input type="submit" id="submit" value="Create New Order" class="button"/> </form>

The default type for a <button> is submit . <button>的默认类型是submit Explicity set it to button and it shouldn't submit the form by default:显式将其设置为button ,默认情况下不应提交表单:

<button type="button" onclick="addproduct()">Add Product</button>

By default, pages are refreshed/forms submitted when buttons are clicked.默认情况下,单击按钮时会刷新页面/提交表单。 You can fix this by adding the attribute type="button" to your buttons.您可以通过将属性type="button"添加到按钮来解决此问题。

 <button type="button" onclick="myFunction()">My button</button>

Prevent using <button> element in the <Form> elements,防止在<Form>元素中使用<button>元素,

By default it's behavior is equivalent to <input type="submit"> element.默认情况下,它的行为等同于<input type="submit">元素。

So Prefer this所以更喜欢这个

<input type="button" value="Add Product">

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

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