简体   繁体   English

如果未满足最小和最大条件,则阻止表单提交

[英]prevent form form submitting when if min and max condition is not fullfilled

I am using javascript to display an error when user click on form submit button if the number entered is not in between min and max.如果输入的数字不在最小值和最大值之间,我正在使用 javascript 在用户单击表单提交按钮时显示错误。 The issue is when you click submit button the form will submit regardless.问题是当您单击提交按钮时,表单将无论如何提交。 Can you please look at the code below and tell me what I need to add.你能看看下面的代码,告诉我我需要添加什么。 I need to prevent form from submitting if the min and max condition is not fulfilled.如果不满足最小和最大条件,我需要阻止表单提交。

this is my PHP codes这是我的 PHP 代码

 <div class="clearfix"></div>
<form action="store_items.php" method="post" name="cartform">
<div class="col-12 form-group">
  <div class="row">
    <div class="col-3">
      <input type="hidden" name="cart_name" value="<?php echo $row['product_title']; ?>">
      <input type="hidden" name="cart_image" value="<?php echo $row['product_photo']; ?>">
      <input id="quantity"type="number" name="cart_qty" step="1" class="form-control text-center" onchange="restrictValue(this,<?php echo $min; ?>, <?php echo $max; ?>)">
      <small class="float-right"><?php echo $row['product_unit']; ?></small>
      <input type="hidden" name="cart_unit" value="<?php echo $row['product_unit']; ?>">
      <input type="hidden" name="cart_price" value="<?php echo $row['product_price']; ?>">
      <input type="hidden" name="product_id" value="<?php echo $row['id']; ?>">
      <input type="hidden" name="seller_id" value="<?php echo $row['seller_id']; ?>">
    </div>

    <div class="col-9">
      <input id="addtocart" class="btn btn-block addtocart" type="submit" value="Add to cart" name="addtocart" <?php if($row['product_status'] == 0) echo 'disabled="disabled"'; ?>  >
    </div>
  </div>
</div>
</form>

this is the javascript这是 javascript

<script type="text/javascript">
  
  function restrictValue(inputElement,minimum,maximum)
{
    let value = inputElement.value;
    if (value < minimum) {
      value = minimum;
      alert('Your order has not reach the minimum order quantity');
      return false;
    }
    if (value > maximum) {
      value = maximum;
      alert('Your order has exceed the avaliable stock');
      return false;
    }
    inputElement.value = value;
}

</script>

You don't need to prevent form submission!您无需阻止表单提交! You can specify the min and max attribute on <input type="number"> and it will prevent users from entering in anything outside those bounds!您可以在<input type="number">上指定minmax属性,它将阻止用户输入超出这些范围的任何内容!

<input id="quantity"type="number" name="cart_qty" step="1" class="form-control text-center" min="<?php echo $min; ?>" max="<?php echo $max; ?>">

But, if you really want to prevent form submission...但是,如果你真的想阻止表单提交......

<form onsubmit="doValidate">
   ...
</form>
<script>
function doValidate(event) {
   event.preventDefault();
   // validate your inputs
};
</script>

You are trying to prevent submission of form, for that you have to look into onSubmit event of form您正试图阻止提交表单,因为您必须查看表单的 onSubmit 事件

Here we can add id to form这里我们可以给form添加id

<div class="clearfix"></div>
<form action="store_items.php" method="post" name="cartform" id="cartfrom">
<div class="col-12 form-group">
  <div class="row">
    <div class="col-3">
      <input type="hidden" name="cart_name" value="<?php echo $row['product_title']; ?>">
      <input type="hidden" name="cart_image" value="<?php echo $row['product_photo']; ?>">
      <input id="quantity"type="number" name="cart_qty" step="1" class="form-control text-center" onchange="restrictValue(this,<?php echo $min; ?>, <?php echo $max; ?>)">
      <small class="float-right"><?php echo $row['product_unit']; ?></small>
      <input type="hidden" name="cart_unit" value="<?php echo $row['product_unit']; ?>">
      <input type="hidden" name="cart_price" value="<?php echo $row['product_price']; ?>">
      <input type="hidden" name="product_id" value="<?php echo $row['id']; ?>">
      <input type="hidden" name="seller_id" value="<?php echo $row['seller_id']; ?>">
    </div>

    <div class="col-9">
      <input id="addtocart" class="btn btn-block addtocart" type="submit" value="Add to cart" name="addtocart" <?php if($row['product_status'] == 0) echo 'disabled="disabled"'; ?>  >
    </div>
  </div>
</div>
</form>

In js you can listen to submit event, do validation whatever you want在js中你可以监听提交事件,做任何你想做的验证

<script type="text/javascript">
  
  function restrictValue(inputElement,minimum,maximum)
{
    let value = inputElement.value;
    if (value < minimum) {
      value = minimum;
      alert('Your order has not reach the minimum order quantity');
      return false;
    }
    if (value > maximum) {
      value = maximum;
      alert('Your order has exceed the avaliable stock');
      return false;
    }
    inputElement.value = value;
}

function logSubmit(event) {
  if(!restrictValue(inputElement,minimum,maximum)) {
    event.preventDefault();
  }
}


const form = document.getElementById('form');
form.addEventListener('submit', submit);
</script>

You can refer this for onSubmit event.你可以参考这个onSubmit 事件。

There are multiple ways you can solve this problem.有多种方法可以解决这个问题。

  1. You can add a disabled attribute on the submit button.您可以在提交按钮上添加disabled属性。 Remove this attribute until all the fields in the form are valid and your form submission is prevented.删除此属性,直到表单中的所有字段都有效并且您的表单提交被阻止。 This might be a bit cumbersome because you'll need to watch for changes on all the form fields and then apply or remove the attribute.这可能有点麻烦,因为您需要观察所有表单字段的更改,然后应用或删除该属性。

  2. Create a custom submit function using js and attach this as the onsubmit event handler.使用 js 创建一个自定义提交 function 并将其附加为onsubmit事件处理程序。 But you'll need an understanding of ajax calls.但是您需要了解 ajax 调用。 eg:例如:

function handleSubmit(evt) {
 evt.preventDefault(); // this will prevent the form from submitting
 if(allFormFieldsAreValid) {
   await fetch('/post-url', // the route you want to post data to 
   {
    method: 'POST',
    body: JSON.stringify(data) // the form data
  }).then(function (data){
   location.replace('/some-url'); // you can redirect to your disered route
  });
 }
}
<form onsubmit="handleSubmit">
  ...
</form>

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

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