javascript - PHP表单在提交之前检查PHP x + y > z
问题描述
我有以下变量:
$aantalcompleet
$ready
$totaalaantal
$aantalcompleet
是从 DB 获取的值。
$totaalaantal
也是从 DB 获得的值。
$ready
是从表单中获取的值。
我想在提交表单之前检查 $aantalcompleet + $ready > $totaalaantal。
如果此总和为 TRUE,我希望收到一条带有“继续或取消”的确认消息。如果总和为 FALSE,则表单可以在没有消息的情况下进行汇总。
我的表格:
<form action="" method="POST">
<div class="col-lg-3">
<div class="input-group">
<span class="input-group-btn">
<button class="btn btn-default" type="button" name="reset_form" value="Reset Form" onclick="this.form.reset();"><i class="fa fa-trash-o"></i></button>
</span>
<input type="number" class="form-control" name="complete" id="complete" value="" placeholder="0">
<input type="hidden" name="machine" value="<?php echo $machine;?>">
<input type="hidden" name="base" value="<?php echo $base;?>">
<input type="hidden" name="lot" value="<?php echo $lot;?>">
<input type="hidden" name="split" value="<?php echo $split;?>">
<input type="hidden" name="sub" value="<?php echo $sub;?>">
<input type="hidden" name="seq" value="<?php echo $seq;?>">
</div>
</div>
<button class="btn btn-default" style="height:35px;width:200px" type="submit" value="gereed" name="gereed">Gereed</button>
解决方案
将此添加到您的页面中的某处<head>
<script>
function onsubmit(event){
if($aantalcompleet + $ready > $totaalaantal){
if(!confirm('Continue or Cancel')){
event.preventDefault();
return false;
}
return true;
}
return true;
}
</script>
假设您的 PHP 变量$aantalcompleet
,$totaalaantal
和$ready
与您的表单在同一个文件中可用,请将以下代码放置在这些变量具有其预期值的位置(即它们填充有数据库中的值,可能就在您的 sql 查询之后(如果有) )
<script>
var $aantalcompleet = <?php echo $aantalcompleet; ?>;
var $totaalaantal = <?php echo $totaalaantal; ?>;
var $ready = <?php echo $ready; ?>;
</script>
然后添加onsubmit="return onsubmit(event)"
到您的表单标签中,例如
<form action="" method="POST" onsubmit="return onsubmit(event)">
推荐阅读
- android - 如果已经作为根在后台堆栈中,Android 活动还没有开始?
- swift - cocapods 不下载 core plot 2.3 版本
- swift - 在 ARKit 和 RealityKit 中使用 Raycast 的真正好处是什么?
- vim - 如何在 vim 缩写中转义字符?
- angular - matTooltip 没有响应长按操作
- python - 我不完全理解这部分代码
- twig - Zikula CMS -- 查找用户属性
- reactjs - 使用酶时如何从路由器模拟位置
- pointers - 具有可分配或指针属性的无限多态虚拟参数?
- java - 如何在与正则表达式模式不匹配的字符串中查找字符